我需要对 REST 服务执行 http GET 并将正文包含在GET
. 不幸的是,#setDoOutput( true )
连接上的设置会强制POST
. 有没有办法送一个GET
尸体?
编辑: 我尝试发送的正文是 JSON。
老板要我们发送一个带有参数的 HTTP GET 请求体。我无法弄清楚如何使用 org.apache.commons.httpclient.methods.GetMethod 或 java.net.HttpURLConnection; 来做到这一点。
GetMethod 似乎不接受任何参数,我不确定如何为此使用 HttpURLConnection。
我正在向我的 API 成功发出以下 curl 请求:
curl -v -X GET -H "Content-Type: application/json" -d {'"query":"some text","mode":"0"'} http://host.domain.abc.com:23423/api/start-trial-api/
Run Code Online (Sandbox Code Playgroud)
我想知道如何从 JAVA 代码内部发出此请求。我尝试通过 Google 和堆栈溢出搜索解决方案。我所发现的只是如何通过查询字符串发送数据或如何通过 POST 请求发送 JSON 数据。
谢谢
我java.lang.IllegalStateException: Already connected
在尝试运行以执行HTTPS GET请求时遇到异常HttpsURLConnection API
.
请在下面找到代码:
HttpsURLConnection con = null;
try {
URL obj = new URL(url);
con = (HttpsURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("Authorization", header);
con.setRequestProperty ("Content-Type", "application/x-www-form-urlencoded");
String urlParameters = "schema=1.0&form=json&byBillingAccountId={EQUALS,cesar@abc.org}";
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("Response Code = " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close(); …
Run Code Online (Sandbox Code Playgroud) 我正在开发一个调用RESTful api的程序.api的所有文档都是cURL命令,但我无法生成cURL命令,所以我需要翻译它们并以不同的方式提出请求.这是他们为我想要进行的查询提供的示例代码.
curl -u '{userEmail}:{userApiToken}' -v -X GET -H 'Content-Type: application/xml' -o 'result.xml' -d '<request><layout>1</layout><searchmode>Cany</searchmode><searchvalue>aaron</searchvalue><filtermode></filtermode><filtervalue></filtervalue><special></special><limit>100</limit><start></start><sortfield></sortfield><sortdir></sortdir></request>' https://secure.website.com/contacts `
Run Code Online (Sandbox Code Playgroud)
我已经阅读了cURL文档,并了解除-d之外的所有标志.我得到它的参数是搜索参数的xml,但是-G在GET cURL上是什么意思?
谢谢
我正在构建一个简单的 REST API(使用PouchDB和Vue.js)。现在,我可以创建projects
几个字段:
服务器.js:
var express = require('express')
var PouchDB = require('pouchdb')
var app = express()
var db = new PouchDB('vuedb')
app.post('/projects/new', function(req, res) {
var data = {
'type': 'project',
'title': '',
'content': '',
'createdAt': new Date().toJSON()
}
db.post(data).then(function (result) {
// handle result
})
})
Run Code Online (Sandbox Code Playgroud)
客户端.js:
// HTML
<input type="text" class="form-control" v-model="title" placeholder="Enter title">
<input type="text" class="form-control" v-model="content" placeholder="Enter content">
<button class="btn btn-default" v-on:click="submit">Submit</button>
// JS
submit () {
this.$http.post('http://localhost:8080/projects/new').then(response => { …
Run Code Online (Sandbox Code Playgroud) 当方法senderform
为 POST 时,一切正常。但是,一旦我将方法更改为 GET,我就不会在服务器上收到任何内容。
function ajaxSubmit(destinationElement, senderform) {
var xmlreq = new XMLHttpRequest();
var params = new FormData(senderform);
xmlreq.open(senderform.method, senderform.action, true);
if (/\/content\.php$/.test(senderform.action))
xmlreq.onreadystatechange = receiveTable;
else xmlreq.onreadystatechange = receiveText;
xmlreq.send(params);
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以在 Action 地址的末尾手动附加键值对,但问题是我不知道哪个表单将与哪些字段一起传递。
如果可能的话,我更喜欢原生 javaScript。
如何使用 XMLHttpRequest 和来自 senderform 的键值对发送 GET 请求,该键值对指向表单元素(与它已经适用于 POST 请求的方式相同)?
假设我有:
@GET
public UserList fetch(@PathParam("user") String userId) {
// Do stuff here
}
Run Code Online (Sandbox Code Playgroud)
现在,假设我有自己的类型userId
,我们称之为UserId
。是否可以将其解析String
为UserId
将其传递到fetch
方法中,即:
@GET
public UserList fetch(@PathParam("user") UserId userId) {
// Do stuff here
}
Run Code Online (Sandbox Code Playgroud)
我意识到一旦进入方法,我就可以解析字符串,但是我的方法获取我想要的类型会更方便。
我需要在 Android 应用程序上构建流量监视器,并且需要存储通过改造发送和接收的所有 json 的大小。使用日志我可以看到它的实际大小,但我还没有找到一种方法来获取此信息以便保存它。我也无法获得response.raw,因为它已经被解析到我的类中。有什么办法可以实现这一点吗?
编辑:将 vadkou 答案标记为最佳答案。
我没有创建新的拦截器,而是传递了 lamda 表达式:
httpClient.addInterceptor( chain -> {
okhttp3.Request request = chain.request();
okhttp3.Response response = chain.proceed(request);
if(request.body()!=null) {
long requestLength = request.body().contentLength();
Log.e("SERVICE GENERATOR", " CONTENT LENGTH" + requestLength);
}
long responseLength = response.body().contentLength();
Log.e("SERVICE GENERATOR", " RESPONSE LENGTH" + responseLength);
return response;
});
Run Code Online (Sandbox Code Playgroud) 如何使用休息模板来获取身体?
基于以下问题:POST request via RestTemplate in JSON,我尝试通过 HttpEntity 使用 body 进行 GET (只需检查是否可能),但接收失败:
缺少必需的请求正文
对于 HttpMethod.POST:localhost:8080/test/post主体已正确添加,但对于 HttpMethod.GET localhost:8080/test/get它未映射。我的代码如下:
@RestController
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
private final RestTemplate restTemplate = new RestTemplate();
@GetMapping("/test/{api}")
public SomeObject test(@PathVariable("api") String api) {
String input = "{\"value\":\"ok\"}";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<>(input, headers);
HttpMethod method = "get".equals(api) ? HttpMethod.GET : HttpMethod.POST;
String url = "http://localhost:8080/" …
Run Code Online (Sandbox Code Playgroud) java ×5
rest ×4
android ×2
curl ×2
get ×2
javascript ×2
json ×2
ajax ×1
api ×1
dropwizard ×1
express ×1
forms ×1
html ×1
jersey-2.0 ×1
pouchdb ×1
resttemplate ×1
retrofit ×1
servlets ×1
spring-boot ×1
vue.js ×1
xml ×1