DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

基于HTTP协议收发JSON数据


使用httpbin测试HTTP POST收发数据。

curl默认的Content-Type是application/x-www-form-urlencoded, 所以第一次发送时JSON数据被当成了form数据, 第二次发送时通过-H参数指定Content-Type为application/json, JSON数据被正确地发出。

$ curl -i -XPOST https://httpbin.org/post --data '{"query": {"a":1,"b":2} }'
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 23 Jan 2017 02:16:03 GMT
Content-Type: application/json
Content-Length: 383
Connection: keep-alive
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

{
  "args": {},
  "data": "",
  "files": {},
  "form": {
    "{\"query\": {\"a\":1,\"b\":2} }": ""
  },
  "headers": {
    "Accept": "*/*",
    "Content-Length": "25",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.35.0"
  },
  "json": null,
  "origin": "61.50.125.214",
  "url": "https://httpbin.org/post"
}

$ curl -i -XPOST -H "Content-Type: application/json" https://httpbin.org/post --data '{"query": {"a":1,"b":2} }'
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 23 Jan 2017 02:13:16 GMT
Content-Type: application/json
Content-Length: 402
Connection: keep-alive
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

{
  "args": {},
  "data": "{\"query\": {\"a\":1,\"b\":2} }",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Content-Length": "25",
    "Content-Type": "application/json",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.35.0"
  },
  "json": {
    "query": {
      "a": 1,
      "b": 2
    }
  },
  "origin": "61.50.125.214",
  "url": "https://httpbin.org/post"
}

Content-Type也可以写作application/json; charset=UTF-8, 但貌似没什么用,UTF-8是默认编码。

对应的Groovy脚本:

import groovy.json.JsonSlurper

def urlString = "https://httpbin.org/post"
def queryString = '{"第一": 3, "第二": 45, "abc": 345345}'

def url = new URL(urlString)
def connection = url.openConnection()
connection.setRequestMethod("POST")
connection.doOutput = true
connection.setRequestProperty('Content-Type', 'application/json')

def writer = new OutputStreamWriter(connection.outputStream)
writer.write(queryString)
writer.flush()
writer.close()
connection.connect()

def jsonSlurper = new JsonSlurper()
def res = jsonSlurper.parseText(connection.content.text)
println "result keys: ${res.keySet()}"
println "json: ${res.json}"
println "json/key: ${res.json['第一']}"
println "headers/Accept: ${res.headers.Accept}"
println "headers/Content-Type: ${res.headers['Content-Type']}"

输出结果:

result keys: [args, data, files, form, headers, json, origin, url]
json: [abc:345345, 第一:3, 第二:45]
json/key: 3
headers/Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
headers/Content-Type: application/json

Ref:

groovy post to a url

java.net.Class URL

http://stackoverflow.com/questions/21404252/post-request-send-json-data-java-httpurlconnection

http://stackoverflow.com/questions/1945336/how-to-set-content-type-on-httpurlconnection



Published

Jan 23, 2017

Last Updated

Jan 23, 2017

Category

Tech

Tags

  • client 3
  • http 4
  • json 7
  • rest 4

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor