I need to write a script similar to this cURL
curl -X 'POST' 'url/authenticate' -H 'accept: */*' -H 'Content-Type: multipart/form-data' -F 'Username=user_name' -F 'Password=pass_word'
I have this, but Request Response Code 400 says that Password field is required and Username field is required
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.ContentType
import groovyx.net.http.Method
def http = new HTTPBuilder('url/authenticate')
def body = [
"Username": 'user_name',
"Password": 'pass_word'
]
http.request(Method.POST, ContentType.URLENC) {
headers.'Accept' = '*/*'
body = body
response.success = { resp, reader ->
log.warn("Response Status: ${resp.statusLine}")
log.warn("Response Data: ${reader}")
}
response.failure = { resp, reader ->
log.warn("Error Response Status: ${resp.statusLine}")
log.warn("Error Response Data: ${reader}")
}
}
How correctly pass form fields in this script?
P.S. POST request works fine from Postman, that provide this form fields in request body
public static String getAuthToken(){
def http = new HTTPBuilder(url + "some/url")
http.request (POST) { multipartRequest ->
MultipartEntityBuilder multipartRequestEntity = new MultipartEntityBuilder()
String key =
multipartRequestEntity.addPart('Username', new StringBody(user))
multipartRequestEntity.addPart('Password', new StringBody(password))
multipartRequest.entity = multipartRequestEntity.build()
response.success = { resp, data ->
return data['token']
}
response.failure = { resp, data ->
return data
}
}
}
U can perform form filling by using MultipartRequestEntityBuilder