あまりに情報がないので一応メモ。
ポイント
- /wp-json/wp/v2/mediaにPOSTで投げる
- フォームの画像のキーは
file
- Content-Typeは
multipart/form-data
- 認証はJWTトークンでやる
CURL
curl -X POST \
https://example.com/wp-json/wp/v2/media \
-H 'Authorization: Bearer XXXXXXXXXXXX' \ # JWTトークン
-H 'Content-Type: multipart/form-data' \ # バイナリでもできるけど今回はフォームで
-H 'cache-control: no-cache' \
-H 'content-disposition: attachment; filename=filename=name.jpeg' \ # nameは任意のファイル名に
-H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
-F 'file=@/Users/user/Documents/name.jpeg' # ファイルのパス
Python
import http.client
conn = http.client.HTTPConnection("example,com")
payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"; filename=\"name.jpeg\"\r\nContent-Type: image/jpeg\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
headers = {
'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
'Content-Type': "multipart/form-data",
'content-disposition': "attachment; filename=filename=name.jpeg",
'Authorization': "Bearer XXXXXXXXXXX",
'cache-control': "no-cache",
}
conn.request("POST", "wp-json,wp,v2,media", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
Ruby on Rails
require 'uri'
require 'net/http'
url = URI("https://example.com/wp-json/wp/v2/media")
http = Net::HTTP.new(url.host, url.port)
request = Net::HTTP::Post.new(url)
request["content-type"] = 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'
request["Content-Type"] = 'multipart/form-data'
request["content-disposition"] = 'attachment; filename=filename=name.jpeg'
request["Authorization"] = 'Bearer XXXXXXXXXXXX'
request["cache-control"] = 'no-cache'
request.body = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"; filename=\"name.jpeg\"\r\nContent-Type: image/jpeg\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
response = http.request(request)
puts response.read_body