NutzCN Logo
问答 在使用http工具用到raw,post提交(application/json)
发布于 2805天前 作者 lambert 6445 次浏览 复制 上一个帖子 下一个帖子
标签:

大神,我在用http工具,用到raw,post提交要怎么提交呢?而且要选择是application/json

6 回复

设置一下header

这我样提交,还是会报401的错误,但是在postMan是有效的。

Request req = Request.create("http://127.0.0.1:8080/api/login/service", METHOD.POST);
        Header header = Header.create();
        header.set("Authorization", "Bearer TcndRr8EMrXvfktJGHxlDjs2dsYDlxdM");
        header.set("OriginFrom", "2_T");
        header.set("Content-Type", "application/json");
        header.set("cache-control","no-cache");

        header.set("data", "{ \"phone\":\"18516520078\", \"password\":\"qaz123456\",  \"userIp\":\"196.0.0.1\",  \"loginTime\":\"2017-12-31 20:08:59\" }");
        Response resp = Sender.create(req).send();
        if (!resp.isOK()) {

        }

再贴一段postMan给到的

OkHttpClient client = new OkHttpClient();

MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, " { \"phone\":18516520078, \"password\":\"qaz123456\",  \"userIp\":\"196.0.0.1\",  \"loginTime\":\"2017-12-31 20:08:59\" }");
Request request = new Request.Builder()
  .url("http://127.0.0.1:8080/api/login/service")
  .post(body)
  .addHeader("authorization", "Bearer TcndRr8EMrXvfktJGHxlDjs2dsYDlxdM")
  .addHeader("originfrom", "2_T")
  .addHeader("content-type", "application/json")
  .addHeader("cache-control", "no-cache")
  .addHeader("postman-token", "7d1e9066-fd19-d896-dd90-e8dea37cb231")
  .build();

Response response = client.newCall(request).execute();
req.setData("{ \"phone\":\"18516520078\", \"password\":\"qaz123456\",  \"userIp\":\"196.0.0.1\",  \"loginTime\":\"2017-12-31 20:08:59\" }");

我用的是nutz-1.r.59,是没有setData的方法的,是要升到61吗?

对不起,我看错了,我知道了

问题解决了,又一次是老神仙帮助解决的,再次感谢wendal天神守护,结贴。

添加回复
请先登陆
回到顶部