Mautic Community Forums

Create Lead on API - State field produces error.

Creating a Lead through the API, we get an error when we try to set the ‘State’ field. We tried values both of ‘CA’ and ‘California’ but both trigger a ‘401 Bad Request’.



How can we set the State correctly when using the API?

Creating a Lead through the API, we get an error when we try to set the ‘State’ field. We tried values both of ‘CA’ and ‘California’ but both trigger a ‘401 Bad Request’.

How can we set the State correctly when using the API?

I was able to create a new field with state filled in via API Tester. There must be another problem with your API call.

@escopecz Are you sure, that this lead’s state is California now? If you check the response, you will see error in state field, but lead will be added. If you check lead on UI, you won’t see lead’s state, it will be empty. So I’m not sure that the question may be closed.

Yes, I’m sure. Here is how the lead looks like in the Leads table and when I see the edit form, I can see the state selected.

Thanks for reply. Could you please paste the exact request from api tester(using Fiddler or so), so I can see how this filed was passed? Look like I send the state field in some another format than yours.

You can use the API tester linked in my first post as well which I think is better option than to use me as an intermediate. There is more debug info you can find helpful. Follow these steps:

  1. cd /var/www/html (or whatever you use for your local development)
  2. git clone git@github.com:mautic/api-library.git
  3. Go to http://localhost/api-library/apitester/index.php (or whenever it got cloned)
  4. Create Mautic API credentials with callback to http://localhost/api-library/apitester/index.php
  5. Fill in the local Mautic URL
  6. Press Submit button (the URL get saved to the session)
  7. Insert oAuth keys created in step 4.
  8. Fill in the API endpoint you want to use. In your case it is leads/new
  9. Change HTTP method from GET to POST (which is required for this endpoint.
  10. Create some Parameters to create a lead with. You can use similar like in my first image.
  11. Submit and view the response

Well, it’s not so easy to launch api tester on Win PC for .Net engineer :slight_smile: After few hours I managed to run php local server to host api tester. But now it fails with error

[code]Warning: require(F:api-library-masterapi-library-master/vendor/autoload.php): failed to open stream: No such file or directory in F:api-library-masterapi-library-masterapitesterindex.php on line 5

Fatal error: require(): Failed opening required ‘F:api-library-masterapi-library-master/vendor/autoload.php’ (include_path=’.;C:phppear’) in F:api-library-masterapi-library-masterapitesterindex.php on line 5[/code]

I tried to install Composer, but no luck:( So it would be really great if you can just show the request which contains state field.

Thanks

Here’s the full log