Skip to Content

401 error while sending messages via HTTP


I am getting 401 error while sending messages via HTTP POST.  Below are the steps I have followed as per the doc

1. Deploy MMS

2. Assign IoT-MMS-user role to my user

3. Under authentication tab, checked the ‘user and password’ selection (required to get response as success message instead of login page)

4. Created Device type, devices and message types

5. Using the url[device id] to send message

The application I am using is iOS native application. Below is the code snippet.

NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:                                                  @”[deviceid]“]                                cachePolicy:NSURLRequestUseProtocolCachePolicy  timeoutInterval:60.0];


             [request setHTTPMethod:@”POST”];

             [request setValue:@”application/json” forHTTPHeaderField:@”Content-Type”];

             [request setHTTPBody:postData];

             NSString* token = @”[MyToken]”;

             NSData* bearerToken = [token dataUsingEncoding:NSUTF8StringEncoding];

               NSString *authValue = [NSString stringWithFormat:@”Bearer %@”, [bearerToken base64EncodedDataWithOptions:4]];

             [request setValue:authValue forHTTPHeaderField:@”Authorization: %@”];


             NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

Please tell me what I am doing wrong here.



You must be Logged on to comment or reply to a post.
  • Hi Prathvi,

    it seems that you are setting the header field for the OAuth token not correctly. Furthermore, the content type should be different. Try to use the following:

    [request setHTTPMethod:@"POST"];

    [request setValue:@"application/json;charset=utf-8" forHTTPHeaderField:@"Content-Type"];

    [request setValue:[NSString stringWithFormat:@"Bearer %@", [MyToken]] forHTTPHeaderField:@"Authorization"];

    [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long) [postData length]] forHTTPHeaderField:@"Content-Length"];

    Just let me know if this works.

    Best regards,


    • Hi Michael,

      I am unable to build the project if i send the token without encoding. It says expected identifier after [MyToken]. I am using xcode version 6.2 



      • Hi Prathvi,

        Please ensure that the OAuth token ([MyToken]) is of type NSString. Maybe you try first to add the token hardcoded, e.g.:

        [request setValue:[NSString stringWithFormat:@"Bearer %@", @"aba0f9e6baf8e429ea336aa2e68569a"] forHTTPHeaderField:@"Authorization"];

        Best regards,


        • Hi Michael,

          Yes, absolutely correct. I am able to send data successfully.

          Thanks for your time and consideration.



        • Hi Michael,

          Is it possible to do HTTP GET request for all the devices which belongs to one device type at a time.



        • Hi Michael,

          I am trying to use send data using the hcp-iot-api using node.js and following is the code :

          var API = require("hcp-iot-api");

          var mms = new API.MessageManagementService({

              "account": "<username>",        // This one is mandatory!

              "password": "<password>",

              "deviceToken": "<deviceToken>",

              "deviceId": "<deviceId>",

              "oauthToken": "<oauthToken>"});


            "messageType": "<messagetype_id>",

            "messages": [


                "sensor1": "Value 1",

                "sensor2": "Value 2"    }



          .catch(function(err) { console.log(err.message); });

          I have registered the device, created message type and device type using the IoT cockpit.

          I get error

          Error "Access to the requested resource is not authorized (HTTP 401)

          Please guide me!!