1

I have an API endpoint that requires data to be sent to it as form-data. I am using RestClient for sending the data. The problem is that I do not manage to give the data for the request in a way, that the endpoint would be able to read it. This is what I have tried:

RestClient::Request.execute(
    method: :post,
    url: url,
    payload: {
      multipart: true,
      data: {
         "user": "test",
         "phase": "test",
         "job_id": "test"
      }
    },
    headers: { Authorization: 'Basic ' + Base64.encode64(auth_details), content_type: "multipart/form-data"}
  )

Any advices how I could add the hash-data for the request in a way that the endpoint could read it as a form?

EDIT1:

The endpoint does receive the request. The endpoint is written with Flask and it tries to read the form parameters using flask.request in the following way:

request.form.get("user")
Leero11
  • 365
  • 2
  • 4
  • 17
  • So what is the actual problem with what you have tried? – jamesc Oct 20 '21 at 06:54
  • When the endpoint tries to read the values user, phase and job_id from the form, it receives null for each of them. – Leero11 Oct 20 '21 at 06:59
  • 1
    So the request actually reaches the end point? You need to post the params that the endpoint actually receives, this is most likely going to be related to structure of the hash differences with what is expected by the end point – jamesc Oct 20 '21 at 08:31
  • 1
    Also do you need a multipart true option as you are not actually sending any resources? – jamesc Oct 20 '21 at 08:34
  • Yes the request does reach the endpoint. I'll try removing the multipart option and see if it changes anything. – Leero11 Oct 20 '21 at 09:43
  • 1
    Can you post the parameters that are actually received by the end point please – jamesc Oct 20 '21 at 16:33
  • 1
    Thanks for the help, managed to solve it! – Leero11 Oct 21 '21 at 08:47
  • 1
    Nice one, you should post your solution as an answer – jamesc Oct 22 '21 at 03:01

1 Answers1

1

I managed to get the request working by changing the reuquest to be the following:

RestClient::Request.execute(
   method: :post,
   url: url,
   payload: {
      "user": "test",
      "phase": "test",
      "job_id": "test"
   },
   headers: { Authorization: 'Basic ' + Base64.encode64(auth_details), 
   content_type: "multipart/form-data"}
)
Leero11
  • 365
  • 2
  • 4
  • 17