Skip to content

Getting 401 Errors with PUT But Getting 200 with GET #151

Open
@BarryDuggan

Description

@BarryDuggan

So I'm making requests to a server that requires an Authorisation header.

I can successfully call the GET endpoints setting the auth header.

GET REQUEST

client.beginRequest();
  client.get("/api/v1/myGetRequest");
  client.sendHeader("Authorization", "Basic abc123");
  client.endRequest();
  int statusCode = client.responseStatusCode();
  String response = client.responseBody();
  Serial.print("GET Status code: ");
  Serial.println(statusCode);
  Serial.print("GET Response: ");
  Serial.println(response);

When I attempt a PUT request however I get a 401 status

PUT REQUEST

void postStatus() {
 String postData;
  Serial.println("making POST request");
  if (alarmIsOn == true) {
    postData = "{\"status\":\"alarmOn\"}";
  } else {
    postData = "{\"status\":\"alarmOff\"}";
  }
  String contentType = "application/json";
  client.beginRequest();
  client.put("/api/v1/myPutRequest", contentType, postData);  
  client.sendHeader(HTTP_HEADER_CONTENT_LENGTH, postData.length());
  client.sendHeader("Authorization", "Basic abc123");
  client.sendHeader("accept", "application/json"); 
  
  client.endRequest();
  client.print(postData);
  

  // read the status code and body of the response
  int statusCode = client.responseStatusCode();
  String response = client.responseBody();

  Serial.print("Status code: ");
  Serial.println(statusCode);
  Serial.print("Response: ");
  Serial.println(response);

}

I've verified that the auth token is correct using postman.
Could somebody help me figure out why setting the auth header for GET works fine but doesnt work for PUT?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions