Daily Develope

[Request] Content-Type의 데이터별 http 요청(requests) 코드 예제 본문

Develope/Web

[Request] Content-Type의 데이터별 http 요청(requests) 코드 예제

noggame 2022. 11. 7. 19:52

 

application/json

 

O Curl

$ curl -x POST \
-h 'Content-Type: application/json' \
-h 'x-client-key: my-sample-key' ... \
-d '{"data1":"value1","data2":{"data21":"value21"},"data3":["value31","value32"]}' \
https://mydomain.com/some-api/sample

O Python

import requests

_url = "https://mydomain.com/some-api/sample"
_headers = {
	"Content-Type": "application/json",
	"x-client-key": "my-sample-key",
	... }
_data = {"data1":"value1","data2":{"data21":"value21"},"data3":["value31","value32"]}

res = requests.post(url=_url, headers=_headers, json=_data)

 


 

application/octet-stream

 

O Curl

$ curl -x POST \
-h 'Content-Type: application/json' \
-h 'x-client-key: my-sample-key' ... \
-d '{\"data1\":\"value1\",\"data2\":{\"data21\":\"value21\"},\"data3\":[31,\"value32\"]}' \
https://mydomain.com/some-api/sample

O Python

import requests

_url = "https://mydomain.com/some-api/sample"
_headers = {
	"Content-Type": "application/json",
	"x-client-key": "my-sample-key",
	... }
_data = '{\"data1\":\"value1\",\"data2\":{\"data21\":\"value21\"},\"data3\":[31,\"value32\"]}'

res = requests.post(url=_url, headers=_headers, data=_data.encode("utf8"))
Comments