Frederick C. Lee
Frederick C. Lee

Reputation: 9503

What is the correct POST format to request data from HerokuApp using Tokens?

Scenario: I'm trying to access an online calculator service from a Herokuapp.
Problem: I'm a Heroku & Postman neophyte.

Here's a given url to the service:

Base URL: https://calculator-frontend-challenge.herokuapp.com

This is supposed to be a POST; so I'm also given the following:

POST /calculations

...which creates a new calculation:
● Input: {}
● Output:

{
"id": ​"ecc7ab90-0a59-4168-b1e5-b5cf63edf9fd"
}

Ok... so far, so good.

enter image description here

So I got the id generated and retrieved.

Now I'm trying to attach some tokens per instruction:

enter image description here

via Postman, I tried to add the 'tokens': enter image description here

And ID:
enter image description here I'm lost here.
I need the correct POST format to get something back from herokuapp.

This doesn't require the use of Postman.
I'm merely using it as a development tool.

Attached is the assignment per request. I'm getting partial results but it's flaky.

enter image description here enter image description here

Upvotes: 0

Views: 146

Answers (2)

sidnt
sidnt

Reputation: 300

enter image description here

  1. You need to add the calculationId to the request path itself. It is not meant to be added as an Auth Header.
  2. Tokens are meant to be sent as a json payload in the request body. They are not meant to be sent as query params.

Upvotes: 1

Procrastin8
Procrastin8

Reputation: 4503

A POST command has a body, they don't typically use query params. It looks like you are supposed to hit the root endpoint to get an id that you will use afterwards, this is your response on the first call. Then, you are supposed to use a POST with an appropriate JSON body to build up your calculation.

POST https://calculator-frontend-challenge.herokuapp.com/calculations/ecc7ab90-0a59-4168-b1e5-b5cf63edf9fd/tokens

In your URLRequest, you can add your JSON body. Easiest way in Swift is to use a Codable type and then use a JSONEncoder to create the data.

struct TokenRequest: Codable {
    let type: TokenType
    let value: String

    enum TokenType: String, Codable {
        case operator
        case number
    }
}

let tokenRequest = TokenRequest(type: .number, value: "5")
var request = URLRequest(url: ...)
request.httpBody = try? JSONEncoder().encode(tokenRequest)

You should read up on RESTful services to get a better handle on the basics of how/why this works.

Upvotes: 0

Related Questions