Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bulk user creation has wrong Accept header #91

Closed
franz-josef-kaiser opened this issue May 25, 2016 · 9 comments
Closed

Bulk user creation has wrong Accept header #91

franz-josef-kaiser opened this issue May 25, 2016 · 9 comments

Comments

@franz-josef-kaiser
Copy link

franz-josef-kaiser commented May 25, 2016

Version info

  • intercom-node version: 2.7.0
  • Node version: 5.9.1

Expected behavior

Create users in bulk

Actual behavior

Returns an error list containing an error of type: media_type_not_acceptable

Steps to reproduce

  1. Set up client
  2. Build array of users to add
  3. Set up request
  4. Inspect error.list

Client:

'use strict';

var IntercomAPI = require( 'intercom-client' );

module.exports = new IntercomAPI.Client( {
    appId     : process.env.INTERCOM_APP_ID,
    appApiKey : process.env.INTERCOM_APP_ID_KEY
} )
    .usePromises();

Request:

var client = require( './client.js' );

module.exports = function( members ) {

    var users = [];

    for ( var key in members ) {
        members.hasOwnProperty( key ) && users.push( {
            create : members[ key ]
        } );
    }

    return client.users.bulk( users )
        .then( function( response ) {
            console.info( '[intercom] [INFO] [Users: Add response]',
                JSON.stringify( response )
            );
        } );
}

Logs

Result of the error list:

{
    "errors": [
        {
            "code": "media_type_not_acceptable",
            "message": "The Accept header should send a media type of application/json"
        }
    ],
    "type": "error.list"
}
@franz-josef-kaiser
Copy link
Author

franz-josef-kaiser commented May 25, 2016

Update: I tried to switch away from your client and use the widely spread request package. The result is the same:

    return request.post( {
        uri : 'https://api.intercom.io/bulk/users',
        'auth': {
            'user' : process.env.INTERCOM_APP_ID,
            'pass' : process.env.INTERCOM_APP_ID_KEY,
            'sendImmediately' : false
        },
        headers : {
            'Accept'       : 'application/json',
            'Content-Type' : 'application/json'
        },
        body : { items : members },
        json : true,
        transform : function( body, response, resolveWithFullResponse ) {
            console.info( '[intercom] [INFO] [Setup: transform]',
                JSON.stringify( body )
            );
            return body;
        }
    } )
        .then( function( response ) {
            console.info( '[intercom] [INFO] [Setup: response]', response );
        } )

The transform closure log gives the following output:

[intercom] [INFO] [Setup: transform]
{
    "app_id": "xxxxx",
    "id": "job_xxx",
    "created_at": 1464183095,
    "completed_at": null,
    "closing_at": 1464183995,
    "updated_at": 1464183095,
    "name": "api_bulk_job",
    "state": "running",
    "links": {
        "error": "https://api.intercom.io/jobs/job_xxx/error",
        "self": "https://api.intercom.io/jobs/job_xxx"
    }
}

The actual response is the following:

[intercom] [INFO] [Setup: response] 
{
    app_id       : 'xxxxx',
    id           : 'job_xxx',
    created_at   : 1464183095,
    completed_at : null,
    closing_at   : 1464183995,
    updated_at   : 1464183095,
    name         : 'api_bulk_job',
    state        : 'running',
    links        :  {
        error : 'https://api.intercom.io/jobs/job_xxx/error',
        self  : 'https://api.intercom.io/jobs/job_xxx'
    }
}

Where the links.error is above posted error in logs with media_type_not_acceptable.

@beastawakens
Copy link

@franz-josef-kaiser This appears to be that you're receiving the correct job model response, but when you try to access the error URL, you are not passing the 'Accept' header, so it is returning that response. Try accessing the error URL and passing the correct 'Accept' header with it. Does that help?

@franz-josef-kaiser
Copy link
Author

@edkellena As you can see from above plain request using the npm request package, I set the correct Accept header…

@franz-josef-kaiser
Copy link
Author

Ok, this has been a big time misunderstanding: The fact that links.error is present, does not mean that an error occurred. Instead, this is just a pointer to any possible errors. The job gets queued on Intercom side and has the state of running – for hours or even days. In case an error happened, the url will have the full error available. One just has to perform another request to fetch the list of errors, again with an Accept header of application/json like the rest of the API. The endpoint is not meant to be accessible via a browser. One can try Postman or similar applications to retrieve that data. Or poll the Intercom servers for the state of a job over a longer period and in case it failed, fetch the result and save it somewhere for inspection.

@coopernewby
Copy link

I believe I am having the same issue, using the node intercom client
headers:
{ 'Content-Type': 'application/json',
Accept: 'application/json',
'User-Agent': 'intercom-node-client/2.0.0',
authorization: 'Basic bmkxbmN6ZG46MmQ1YmQ3MzM5NjkyOWI5YWFlNzUxZjJlOGI3NDc5MGYwOWU1ZWVhNA==',
'content-length': 3057 },

@franz-josef-kaiser
Copy link
Author

@coopernewby Have you read my last comment? Job queue and such?

P.s.: Can you please use code formatting? Makes it much easier to read.

@coopernewby
Copy link

Yes sorry about the formatting. I just don't understand how long the bulk jobs seem to run for or how to fix the 'media_type_not_acceptable' error. This is what I am passing in:
bulkUsers.push({ create: { email: output[j][0], user_id: output[j][4], type: output[j][9], name: output[j][2], companies: [{ id: output[j][6] }] } })

@franz-josef-kaiser
Copy link
Author

Are you looking at the request in a browser? Use something like the Postman app instead.

@coopernewby
Copy link

Yes! Got it. I ran this and got much more information on the job about errors or if the bulg job was done curl -u 'app_id':'app_api_key' -H 'Content-Type: application/json' -H 'Accept: application/json' https://api.intercom.io/jobs/job_8de063a0_43cb_11e6_a749_11589fe21ce7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants