我试图通过RestClient ruby API将JSON数据发送到Sinatra应用程序.
在客户端(client.rb)(使用RestClient API)
response = RestClient.post 'http://localhost:4567/solve', jdata, :content_type => :json, :accept => :json
Run Code Online (Sandbox Code Playgroud)
在服务器(Sinatra)
require "rubygems"
require "sinatra"
post '/solve/:data' do
jdata = params[:data]
for_json = JSON.parse(jdata)
end
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
/Library/Ruby/Gems/1.8/gems/rest-client-1.5.1/lib/restclient/abstract_response.rb:53:in `return!': Resource Not Found (RestClient::ResourceNotFound)
from /Library/Ruby/Gems/1.8/gems/rest-client-1.5.1/lib/restclient/request.rb:193:in `process_result'
from /Library/Ruby/Gems/1.8/gems/rest-client-1.5.1/lib/restclient/request.rb:142:in `transmit'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:543:in `start'
from /Library/Ruby/Gems/1.8/gems/rest-client-1.5.1/lib/restclient/request.rb:139:in `transmit'
from /Library/Ruby/Gems/1.8/gems/rest-client-1.5.1/lib/restclient/request.rb:56:in `execute'
from /Library/Ruby/Gems/1.8/gems/rest-client-1.5.1/lib/restclient/request.rb:31:in `execute'
from /Library/Ruby/Gems/1.8/gems/rest-client-1.5.1/lib/restclient.rb:72:in `post'
from client.rb:52
Run Code Online (Sandbox Code Playgroud)
我想要的是发送JSON数据并使用RestClient和Sinatra接收JSON数据.但是无论我尝试什么,我都会收到上述错误.我坚持了3个小时.请帮忙
我正在尝试使用rest-client gem发布一些东西,但由于某种原因,我不断收到内部服务器错误.我在Chrome上使用了Simple REST Client,除非我发送了以下标题,否则会出现同样的错误:
Content-Type: application/x-www-form-urlencoded
Run Code Online (Sandbox Code Playgroud)
所以我正在尝试使用post请求发送该标头,但由于某种原因,它仍然无法正常工作.这是我尝试过的:
RestClient.post "server", :content_type=>"Content-Type: application/x-www-form-urlencoded",:name=> 'Test', :message_type=> 'Request', :version=> '2.0'
RestClient.post "server", {:content_type=> "Content-Type: application/x-www-form-urlencoded"},:name=> 'Test', :message_type=> 'Request', :version=> '2.0'
RestClient.post "server", {"Content-Type" =>"Content-Type: application/x-www-form-urlencoded"},:name=> 'Test', :message_type=> 'Request', :version=> '2.0'
RestClient.post "server", :header => {:content_type=>: "Content-Type: application/x-www-form-urlencoded"},:name=> 'Test', :message_type=> 'Request', :version=> '2.0'
Run Code Online (Sandbox Code Playgroud)
有人能告诉我我做错了什么吗?已搜索过一些指示如何设置标题的文档,但似乎没有任何效果.
REST API不能定义固定资源名称或层次结构(客户端和服务器的明显耦合).服务器必须能够自由控制自己的命名空间.相反,允许服务器通过在媒体类型和链接关系中定义这些指令来指示客户端如何构造适当的URI,例如在HTML表单和URI模板中完成的.
如何为系统到系统接口执行此操作?假设客户想要在服务器上创建一个订单http://my.server.org它应该如何知道创建订单它应该使用网址http://my.server.org/newOrder而不是http://my.server.org/nO或其他什么?
对于人机界面(即浏览器),我猜服务器会提供某种形式的链接(可能在一个form元素中),并且该链接中的文本将告诉用户该页面上的哪些表单是正确的订单(应该创建用户或导航到某些搜索结果)
在客户端实现此功能的机制是什么?而且:他们实际使用过,还是大多数人只是将网址硬连接到客户端?
现在我可以提出如下要求:
user = 'xxx'
token = 'xxx'
survey_id = 'xxx'
response = RestClient.get "https://survey.qualtrics.com/WRAPI/ControlPanel/api.php?Request=getLegacyResponseData&User=#{user}&Token=#{token}&Version=2.0&SurveyID=#{survey_id}&Format=XML"
Run Code Online (Sandbox Code Playgroud)
但应该有一些更好的方法来做到这一点.我尝试过这样的事情:
response = RestClient.get "https://survey.qualtrics.com/WRAPI/ControlPanel/api.php", :Request => 'getLegacyResponseData', :User => user, :Token => token, :Version => '2.0', :SurveyID => survey_id, :Format => 'XML'</code>
Run Code Online (Sandbox Code Playgroud)
及其变化(字符串代替键的符号,包括{和},使键小写等)但我试过的组合似乎都没有用.这里的语法是什么?
我尝试了下面的第一个建议.它没用.为了记录,这工作:
surveys_from_api = RestClient.get "https://survey.qualtrics.com/WRAPI/ControlPanel/api.php?Request=getSurveys&User=#{user}&Token=#{token}&Version=#{version}&Format=JSON"
Run Code Online (Sandbox Code Playgroud)
但这不是:
surveys_from_api = RestClient.get "https://survey.qualtrics.com/WRAPI/ControlPanel/api.php", :params => {:Request => 'getSurveys', :User => user, :Token => token, :Version => version, :Format => 'JSON'}
Run Code Online (Sandbox Code Playgroud)
(我设置版本='2.0').
我的请求看起来像这样,但是当我尝试运行此代码时,出现此错误:
@Grab(group='org.codehaus.groovy.modules.httpbuilder',module='http-builder', version='0.7')
import groovy.json.JsonOutput
import groovy.json.JsonBuilder
import groovyx.net.http.RESTClient
import static groovyx.net.http.ContentType.*
import groovyx.net.http.HttpResponseException
public PaymentSpring() throws Exception {
def username ='test_XXXXXXXXXXXXXXXXXXXX'
def resp
def https = new RESTClient('https://api.paymentspring.com/api/v1')
https.auth.basic(username,'')
def charge= [
card_owner_name : 'test tset',
card_number : '345829002709133',
card_exp_month:'12',
card_exp_year : '2015',
csc:'999',
amount:'100.00',
email_address:'ujdieu@yahoo.com',
send_receipt : true
]
try {
resp = https.post(
path:'/charge',
requestContentType: URLENC,//request content synchronously for the "headers"
headers: ['Content-Type': 'application/x-www-form-urlencoded'],
body:charge
)
} catch(HttpResponseException ex) {
println ex
}
println resp
Run Code Online (Sandbox Code Playgroud)
结果:
groovyx.net.http.HttpResponseException: …Run Code Online (Sandbox Code Playgroud) 我是红宝石的新手(也是编程)
我已经构建了这段代码:
#This method executing a url and give the response in json format
def get url
return JSON.parse(RestClient::Request.execute(method: :get, url: url))
end
Run Code Online (Sandbox Code Playgroud)
现在我正在尝试处理一个案例,任何网址的响应代码都不正常,我想用错误消息"error"替换它
我试图用这段代码替换get方法:
def get url
if ((RestClient::Request.execute(method: :get, url: url)).code == 200)
return JSON.parse(RestClient::Request.execute(method: :get, url: url))
else
error = "error"
return error.as_json
end
end
Run Code Online (Sandbox Code Playgroud)
但是如果来自网址的响应不是200,我收到错误消息"406不可接受"而不是"错误"
提前致谢
使用Spring Boot 1.5.4.RELEASE Microservice使用ElasticSearch提供的低级Rest Client连接到ElasticSearch 5.5.0实例.
的pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Elasticsearch -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>5.5.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.5.0</version>
</dependency>
<!-- Apache Commons -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.6</version>
</dependency>
<!-- Jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.9</version>
</dependency>
<!-- Log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- JUnit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version> …Run Code Online (Sandbox Code Playgroud) 我在 VSCode 中使用 REST 客户端。文件扩展名是http.
我们可以使用 hash: #、双斜杠:进行注释//,并使用三重哈希:分隔请求###。
但是我们怎样才能有多行注释呢?
Spring FrameworkRestTemplate文档中有注释:
\n\n注意:截至
\n5.0此类处于维护模式,仅接受较小的\n更改请求和错误。请考虑使用org.springframework.web.reactive.client.WebClient具有更现代的 API 并支持同步、异步和流式传输的方案。
当我尝试使用WebClient并进行同步调用时,如下所示:
@RestController\n@RequestMapping("/rating")\npublic class Controller {\n\n @Autowired\n private RestTemplate restTemplate;\n\n @Autowired\n private WebClient.Builder webClientBuilder;\n\n @RequestMapping("/{userId}")\n public UserRating getUserRating(@PathVariable("userId") String userId) {\n\n// return restTemplate.getForObject("http://localhost:8083/ratingsdata/user/" + userId, UserRating.class);\n return webClientBuilder.build()\n .get().uri("http://localhost:8083/ratingsdata/user/" + userId)\n .retrieve()\n .bodyToMono(UserRating.class)\n .block();\n\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n项目pom.xml:
\n<?xml version="1.0" encoding="UTF-8"?>\n<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">\n <modelVersion>4.0.0</modelVersion>\n <parent>\n <groupId>org.springframework.boot</groupId>\n <artifactId>spring-boot-starter-parent</artifactId>\n <version>2.4.0</version>\n <relativePath/> <!-- lookup parent from repository -->\n </parent>\n <groupId>com.daren.tutorial.movie</groupId>\n <artifactId>catalog</artifactId>\n <version>0.0.1-SNAPSHOT</version>\n <name>catalog</name>\n …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用代理框架构建一个简单的 Resteasy 客户端。我收到错误“无法实例化 ResteasyClientBuilder 类型”。这是客户端类。
package com.RestClient.Clients;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
import com.RestClient.Models.Student;
public class ClientClass {
ResteasyClient client;
ResteasyWebTarget base_target,student_target;
ClientInterface proxy;
public ClientClass() {
client = new ResteasyClientBuilder().build();<---------error
base_target = client.target(UriBuilder.fromPath("http://localhost:8080/demorest/webresources/"));
student_target = base_target.path("students");
}
public int registerStudent(Student s) {
Response res = proxy.createStudent(s);
return res.getStatus();
}
}
Run Code Online (Sandbox Code Playgroud)
我正在关注这个教程。
rest-client ×10
java ×3
ruby ×3
groovy ×1
http-headers ×1
httpbuilder ×1
json ×1
rest ×1
resteasy ×1
sinatra ×1
spring ×1
spring-boot ×1