lundi 7 février 2022

Ruby POST request with an external file attached

Since a week ago, I've been facing this issue, but I've not found the solution. When I send a POST request to an API, it returns

{"errors":[],"message":"A file or fileKey is required","status":400,"type":"BadRequestError"}

This is what I am trying to do:

    url = URI("https://xxx.xxx/v2/attachments")

    https = Net::HTTP.new(url.host, url.port)
    https.use_ssl = true

    request = Net::HTTP::Post.new(url)
    request["X-Application"] = "IDENTIFICATION"
    form_data = [['name', 'my-file.pdf'],['parcelId', '123'],['file', Net::HTTP.get(URI("http://www.africau.edu/images/default/sample.pdf"))]]
    request.set_form form_data, 'multipart/form-data'
    response = https.request(request)
    puts response.read_body

Is something wrong with the way I attach the file?

Aucun commentaire:

Enregistrer un commentaire