Kannan TK
Kannan TK

Reputation: 29

How do we set x-www-form-urlencoded type body in jenkins pipeline/groovy post request?

def body ='{"1":"ab","2":"cd"}'

def response = httpRequest(httpMode: 'POST', url: "https://***/xyz", customHeaders:[[name:"Content-type",value:"application/x-wwww-form-urlencoded"]], body =body)

Expected response is json values.Here always received Html page.

IN Postman, when we choose body type is x-wwww-form-urlencoded , we are getting correct json format response. IN Postman, when we choose any other body type/no body , we are getting html formatted output.

      How do we achieve similar json output via jenkins/groovy script?

genkins website content ::: requestBody : String (optional) The raw body of the request.

   How do we set x-wwww-form-urlencoded type body in jenkins post request?

Upvotes: 1

Views: 2022

Answers (2)

Romain DEQUIDT
Romain DEQUIDT

Reputation: 908

As has said Alex Popov in this comment, you can simply use : contentType: 'APPLICATION_FORM'

But, actually, APPLICATION_FORM is defined with org.apache.http.entity.ContentType.APPLICATION_FORM_URLENCODED (http_request - MimeType.java#L15)

So, you'll ends with application/x-www-form-urlencoded; charset=ISO-8859-1 (apache httpcomponents contenttype

It shouldn't be a problem, but, if you want to restrict your Content-Type HTTP header, you can overwrite it using:

customHeaders: [
  [name: "header1", value: "value1"],
  ...
  [name: "Content-Type", value: "application/x-www-form-urlencoded"],
  ...
  [name: "headerN", value: "valueN"]
]

Upvotes: 0

Alex Popov
Alex Popov

Reputation: 31

def response = httpRequest(
  contentType: 'APPLICATION_FORM',
  httpMode: 'POST',
  customHeaders: [whatever]
  requestBody: 'MyVariableOne=ValueOne&MyVariableTwo=ValueTwo...',
  url: 'https://whatever'
)
println(response.content)

Upvotes: 3

Related Questions