# Upload Media: https://api.x.com/2/media/upload POST https://graph.facebook.com/2/media/upload Content-Type: multipart/form-data Reference: https://docs.deao.dev/cloudflare/social-media-posting/x/upload-image/upload-media-https-api-x-com-2-media-upload ## OpenAPI Specification ```yaml openapi: 3.1.0 info: title: collection version: 1.0.0 paths: /2/media/upload: post: operationId: upload-media-https-api-x-com-2-media-upload summary: 'Upload Media: https://api.x.com/2/media/upload' tags: - >- subpackage_socialMediaPosting.subpackage_socialMediaPosting/x.subpackage_socialMediaPosting/x/uploadImage responses: '200': description: OK content: application/json: schema: $ref: >- #/components/schemas/Social Media Posting_X_Upload Image_Upload Media: https://api.x.com/2/media/upload_Response_200 requestBody: content: multipart/form-data: schema: type: object properties: media: type: string format: binary media_type: type: string media_category: type: string required: - media - media_type - media_category servers: - url: https://graph.facebook.com - url: https://www.youtube.com - url: https://youtube-video-summarizer-gpt-ai.p.rapidapi.com - url: https://api.cloudflare.com - url: https://dev-musiccast.odoo.com - url: https://api.search.brave.com - url: https://og-image-generator-dev.deao.workers.dev - url: https://musiccast.odoo.com - url: https://m.musicca.st - url: https://api.stripe.com - url: https://api.kie.ai - url: https://api.pexels.com - url: https://api.shotstack.io - url: https://oauth2.googleapis.com - url: https://www.googleapis.com - url: https://api.linkedin.com - url: https://www.linkedin.com - url: https://rupload.facebook.com - url: https://api.x.com - url: https://open.tiktokapis.com - url: https://open-upload-sg.tiktokapis.com - url: https://social-media-posting-dev.deao.workers.dev components: schemas: 2MediaUploadPostResponsesContentApplicationJsonSchemaDataImage: type: object properties: h: type: integer w: type: integer image_type: type: string required: - h - w - image_type title: 2MediaUploadPostResponsesContentApplicationJsonSchemaDataImage 2MediaUploadPostResponsesContentApplicationJsonSchemaData: type: object properties: id: type: string size: type: integer image: $ref: >- #/components/schemas/2MediaUploadPostResponsesContentApplicationJsonSchemaDataImage media_key: type: string expires_after_secs: type: integer required: - id - size - image - media_key - expires_after_secs title: 2MediaUploadPostResponsesContentApplicationJsonSchemaData 'Social Media Posting_X_Upload Image_Upload Media: https://api.x.com/2/media/upload_Response_200': type: object properties: data: $ref: >- #/components/schemas/2MediaUploadPostResponsesContentApplicationJsonSchemaData required: - data title: >- Social Media Posting_X_Upload Image_Upload Media: https://api.x.com/2/media/upload_Response_200 ``` ## SDK Code Examples ```python Social Media Posting_X_Upload Image_Upload Media: https://api.x.com/2/media/upload_example import requests url = "https://graph.facebook.com/2/media/upload" files = { "media": "open('string', 'rb')" } payload = { "media_type": "string", "media_category": "string" } response = requests.post(url, data=payload, files=files) print(response.json()) ``` ```javascript Social Media Posting_X_Upload Image_Upload Media: https://api.x.com/2/media/upload_example const url = 'https://graph.facebook.com/2/media/upload'; const form = new FormData(); form.append('media', 'string'); form.append('media_type', 'string'); form.append('media_category', 'string'); const options = {method: 'POST'}; options.body = form; try { const response = await fetch(url, options); const data = await response.json(); console.log(data); } catch (error) { console.error(error); } ``` ```go Social Media Posting_X_Upload Image_Upload Media: https://api.x.com/2/media/upload_example package main import ( "fmt" "strings" "net/http" "io" ) func main() { url := "https://graph.facebook.com/2/media/upload" payload := strings.NewReader("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"media\"; filename=\"string\"\r\nContent-Type: application/octet-stream\r\n\r\n\r\n-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"media_type\"\r\n\r\nstring\r\n-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"media_category\"\r\n\r\nstring\r\n-----011000010111000001101001--\r\n") req, _ := http.NewRequest("POST", url, payload) res, _ := http.DefaultClient.Do(req) defer res.Body.Close() body, _ := io.ReadAll(res.Body) fmt.Println(res) fmt.Println(string(body)) } ``` ```ruby Social Media Posting_X_Upload Image_Upload Media: https://api.x.com/2/media/upload_example require 'uri' require 'net/http' url = URI("https://graph.facebook.com/2/media/upload") http = Net::HTTP.new(url.host, url.port) http.use_ssl = true request = Net::HTTP::Post.new(url) request.body = "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"media\"; filename=\"string\"\r\nContent-Type: application/octet-stream\r\n\r\n\r\n-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"media_type\"\r\n\r\nstring\r\n-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"media_category\"\r\n\r\nstring\r\n-----011000010111000001101001--\r\n" response = http.request(request) puts response.read_body ``` ```java Social Media Posting_X_Upload Image_Upload Media: https://api.x.com/2/media/upload_example import com.mashape.unirest.http.HttpResponse; import com.mashape.unirest.http.Unirest; HttpResponse response = Unirest.post("https://graph.facebook.com/2/media/upload") .body("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"media\"; filename=\"string\"\r\nContent-Type: application/octet-stream\r\n\r\n\r\n-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"media_type\"\r\n\r\nstring\r\n-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"media_category\"\r\n\r\nstring\r\n-----011000010111000001101001--\r\n") .asString(); ``` ```php Social Media Posting_X_Upload Image_Upload Media: https://api.x.com/2/media/upload_example request('POST', 'https://graph.facebook.com/2/media/upload', [ 'multipart' => [ [ 'name' => 'media', 'filename' => 'string', 'contents' => null ], [ 'name' => 'media_type', 'contents' => 'string' ], [ 'name' => 'media_category', 'contents' => 'string' ] ] ]); echo $response->getBody(); ``` ```csharp Social Media Posting_X_Upload Image_Upload Media: https://api.x.com/2/media/upload_example using RestSharp; var client = new RestClient("https://graph.facebook.com/2/media/upload"); var request = new RestRequest(Method.POST); request.AddParameter("undefined", "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"media\"; filename=\"string\"\r\nContent-Type: application/octet-stream\r\n\r\n\r\n-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"media_type\"\r\n\r\nstring\r\n-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"media_category\"\r\n\r\nstring\r\n-----011000010111000001101001--\r\n", ParameterType.RequestBody); IRestResponse response = client.Execute(request); ``` ```swift Social Media Posting_X_Upload Image_Upload Media: https://api.x.com/2/media/upload_example import Foundation let parameters = [ [ "name": "media", "fileName": "string" ], [ "name": "media_type", "value": "string" ], [ "name": "media_category", "value": "string" ] ] let boundary = "---011000010111000001101001" var body = "" var error: NSError? = nil for param in parameters { let paramName = param["name"]! body += "--\(boundary)\r\n" body += "Content-Disposition:form-data; name=\"\(paramName)\"" if let filename = param["fileName"] { let contentType = param["content-type"]! let fileContent = String(contentsOfFile: filename, encoding: String.Encoding.utf8) if (error != nil) { print(error as Any) } body += "; filename=\"\(filename)\"\r\n" body += "Content-Type: \(contentType)\r\n\r\n" body += fileContent } else if let paramValue = param["value"] { body += "\r\n\r\n\(paramValue)" } } let request = NSMutableURLRequest(url: NSURL(string: "https://graph.facebook.com/2/media/upload")! as URL, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10.0) request.httpMethod = "POST" request.httpBody = postData as Data let session = URLSession.shared let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in if (error != nil) { print(error as Any) } else { let httpResponse = response as? HTTPURLResponse print(httpResponse) } }) dataTask.resume() ```