1

I am having following groovy code that makes a post call to get some auth token and it works fine:

def post = new URL("abc.com").openConnection();
def message = '{"username":"user", "password":"pass"}'
post.setRequestMethod("POST")
post.setDoOutput(true)
post.setRequestProperty("Content-Type", "application/json")
post.getOutputStream().write(message.getBytes("UTF-8"));
def postRC = post.getResponseCode();
println(postRC);
if (postRC.equals(200)) {
    println(post.getInputStream().getText());
} 

I now what to make a new post call multipart/form data where i will pass auth token in header and a file in form data. The curl call for the same (generated by postman), looks like follow:

curl --location --request POST 'abc.com/resources' \
--header 'Content-Type: multipart/form-data' \
--header 'Authorization: token' \
--form 'request=@"myfile.yaml"'

Now if i modify the above url request in groovy to support this curl call, i can add content-type in post.setRequestProperty("Content-Type", "multipart/form-data") and same for auth token, how can I set form data in this with request key = file ?

user124
  • 423
  • 2
  • 7
  • 26
  • Does this answer your question? [encoder function for multipart/form-data in groovy](https://stackoverflow.com/questions/28870172/encoder-function-for-multipart-form-data-in-groovy) – Simon Kissane Oct 05 '21 at 01:26

0 Answers0