Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

339
Views
restAssured - cannot master post method

fellow stackoverflowians :)

I've been for quit time to make a Post call using Gmail API. Been trying to use createDraft and createLabel. Now I guess I've found how to do this correctly (mostly) but I get this error:

    java.lang.AssertionError: 1 expectation failed.
Expected status code <200> but was <400>.

I realise that this error occurs because I make incorrect request.

Could You, guys, help me with this? Here's my code:

import io.restassured.RestAssured.*
import io.restassured.http.ContentType
import io.restassured.matcher.RestAssuredMatchers.*
import org.hamcrest.Matchers.*
import org.testng.annotations.Test




class RestAPIAutoTestPost {
    @Test
    fun createLabelInGoogleMail() {
        RestAssured.baseURI = "https://www.googleapis.com/gmail/v1/users/me"
        val accessToken = "ya29.Glw7BEv6***"

        val jsonAsMap = HashMap<String, Any>()
        jsonAsMap.put("id", "labelAPITestNameID")
        jsonAsMap.put("labelListVisibility", "labelShow")
        jsonAsMap.put("messageListVisibility", "show")
        jsonAsMap.put("messagesTotal", "0")
        jsonAsMap.put("messagesUnread", "0")
        jsonAsMap.put("name", "labelAPITestName")
        jsonAsMap.put("threadsTotal", "0")
        jsonAsMap.put("threadsUnread", "0")
        jsonAsMap.put("type", "user")


        given().
                contentType(ContentType.JSON).
                body(jsonAsMap).

        `when`()
                post("/labels?access_token=$accessToken").
        then().
                statusCode(200)
    }

}

I suppose I use HashMap incorrectly or I use some incorrect body fields. I've only started to learn restAssured so I beg my pardons for newby question.

Thanks!

P.S. I'd really appreciate for any help with Post methods and puting data into body

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I think your use of RestAssured and HashMap is correct. I think you are getting a 400 from this API because you are specifying the id property. By playing with this in Google's API Explorer, I was able to generate 400 errors by doing that. According to the documentation, the only things you need to specify for a POST/Create are: labelListVisibility, messageListVisibility, and name. The id is returned to you as part of the response.

A good feature in RestAssured is that you can have it log what it sends or receives when there is an error or all the time.

Log all requests:

given().log().all()

Log all responses:

`when`().log().all()

Or just when validations fail:

`when`().log().ifValidationFails()

Using that will give you a more precise reason why your interaction with the API is failing because it will show whatever Google is sending back. So we can see for sure if I'm right about the id.

And since you seem to be using Kotlin for this, you might want to take advantage of its great multiline string capabilities and just create the JSON payload manually:

val body = """
    {
        "labelListVisibility": "labelShow",
        "messageListVisibility": "show",
        "name": "ThisIsATest"
    }
"""
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!