相关文章推荐
坚强的紫菜汤  ·  java httpclient ipv6 ...·  2 周前    · 
纯真的橙子  ·  在 ASP.NET Core ...·  1 周前    · 
果断的甘蔗  ·  Input Stream Member ...·  4 月前    · 
果断的海豚  ·  在SQLite中使用NOT ...·  9 月前    · 
爱跑步的钥匙  ·  写 SQL 时报错 ...·  1 年前    · 

httpClient post请求设置了Content-Type application/json还是无法转换为json格式,415,stringEntity.setContentType

最新推荐文章于 2024-10-11 17:07:50 发布
最新推荐文章于 2024-10-11 17:07:50 发布 阅读量5.2k

httpClient post请求设置了Content-Type application/json还是无法转换为json格式

问题是这样的,开发时遇到了上述问题,我的代码是这么写的
nodejs 中的请求解析json数据引用

app.use(express.json());
app.use(express.urlencoded({ extended: false }));

java:

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(apiUrl);
JSONObject obj = new JSONObject();
obj.put("filePath", "jpg");
obj.put("projectId", "abc");
StringEntity stringEntity = new StringEntity(obj.toString(), "UTF-8");
stringEntity.setContentEncoding("UTF-8");
stringEntity.setContentType("application/json");
CloseableHttpResponse response = httpClient.execute(httpPost);
int status = response.getStatusLine().getStatusCode()
16:32:09.287 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "{"filePath":"jpg","projectId":"abc"}"
16:32:09.291 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "HTTP/1.1 415 Unsupported Media Type[\r][\n]"

415 Unsupported Media Type 服务器无法处理请求附带的媒体格式
看样子json格式,没错,但是报415错误

于是我在请求头添加

httpPost.addHeader("Content-Type", "application/json; charset=utf-8");

依然报同样的错误,变成

httpPost.addHeader("Content-Type", "application/json; charset=utf-8");
stringEntity.setContentEncoding("UTF-8");
stringEntity.setContentType("application/json");

于是把删掉
stringEntity.setContentEncoding(“UTF-8”);
stringEntity.setContentType(“application/json”);
`

保留
httpPost.addHeader("Content-Type", "application/json; charset=utf-8");
就解决了

问题是解决了,但原因未明,我看其他博客
有时也会用到stringEntity.setContentType(“application/json”);,
难道时header和stringEntity不能同时设置?
但只设置stringEntity又会包415,嗯,有点诡异

后来尝试了几种组合,注意utf-8后有分号

httpPost.addHeader("Content-Type", "application/json; charset=utf-8;");
stringEntity.setContentEncoding("UTF-8");
stringEntity.setContentType("application/json");
httpPost.addHeader("Content-Type", "application/json;");
stringEntity.setContentEncoding("UTF-8");
stringEntity.setContentType("application/json");

以上两种不报415,但是nodejs就没解析到

exports.xxx= (req, res) => {
    logger.info('params:' + json(req.body));
2022-03-04T17:00:14+08:00 [info] : params:{}

看着时发过去了

17:00:14.205 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Content-Type: application/json;[\r][\n]"
17:00:14.205 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Accept-Language: zh-CN,zh;q=0.9,en;q=0.8[\r][\n]"
17:00:14.205 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Connection: keep-alive[\r][\n]"
17:00:14.205 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Content-Length: 56[\r][\n]"
17:00:14.205 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Content-Encoding: UTF-8[\r][\n]"
17:00:14.206 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Host: 127.0.0.1:8956[\r][\n]"
17:00:14.206 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "User-Agent: Apache-HttpClient/4.5.6 (Java/1.8.0_191)[\r][\n]"
17:00:14.206 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "[\r][\n]"
17:00:14.206 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "{"filePath":"jpg","projectId":"abc"}"
httpPost.addHeader("Content-Type", "application/json");
stringEntity.setContentEncoding("UTF-8");
stringEntity.setContentType("application/json");
这个报415

嗯,记录一下

import net.sf.json.JSONObject; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClientBuilder; // StringEntity s = new StringEntity(dataStr); // s.setContentEncoding("UTF-8"); // s.setContentType("application/json");
privatevoidhttpReqUrl(List<HongGuTan>list,Stringurl) throwsClientProtocolException,IOException{ import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.logging.Log; import android.text.TextUtils import android.util.Log import com.alibaba.fastjson.JSONException import com.google.gson.Gson import okhttp3.* import okhttp
requests通过post请求设置headers = {‘Content-Type’: ‘application/json’ }未生效问题 1、requests中,设置headers参数{‘Content-Type’: ‘application/json’ },是将请求数据以json格式发送,而headers中默认请求方式是表单提交:{‘Content-Type’: ‘application/x-www-form-urlencoded’ }; 设置了headers = {‘Content-Ty
结果运行后发现$_POST数组为空! 百度之后发现是content-type为"application/json"的数据php是不能直接识别的,所以导致$_POST数组为空 解决办法:我们只要用$GLOBALS['HTTP_RAW_POST_DATA']把原始数据取出来,然后再json_decode就行了
//request.getParameterNames(); br = new BufferedReader(new InputStreamReader((ServletInputStream) request.getInputStream(), "utf-8")); StringBuffer sb = new StringBuffer(""); String temp; while ((temp = br.readLine()) != null) { sb.ap 第一部分:目前工作中涉及到的content-type 有三种: content-type:在Request Headers里,告诉服务器我们发送的请求信息是哪种格式的。 1content-type:application/x-www-form-urlencoded 默认的。如果不指定content-ty...
由于项目需要,需要用httpclient调其他部门的接口获取信息,学习了httpclient的使用方法,demo大概代码如下: public static List doPost(String url, JSONObject json, String infoname, Class cl){ CloseableHttpClient httpclient = HttpClien
HttpClient client = new HttpClient(); //HttpMethod method = postMethod("https://m.kuaidi100.com/query", expressCompany,expressNum); PostMethod post = new PostMethod("https://m.kuaidi
httpClient post请求设置了Content-Type application/json还是无法转换为json格式,415,stringEntity.setContentType
httpClient post请求设置了Content-Type application/json还是无法转换为json格式,415,stringEntity.setContentType zhuloong: 是的,我也遇到了同样的问题,正在寻找问题的答案 httpClient post请求设置了Content-Type application/json还是无法转换为json格式,415,stringEntity.setContentType 时生-0: 。。。。我也被这个搞糊涂了