snipe-it icon indicating copy to clipboard operation
snipe-it copied to clipboard

Post new asset to api/v1/hardware gives 302

Open rcarterv opened this issue 9 years ago • 2 comments

Expected Behavior (or desired behavior if a feature request)

Posting to http://hostname/api/v1/hardware should create a new asset (using example: https://snipe-it.readme.io/v4.0/reference#hardware-create)

<?php

// set post fields
$data = [
    'asset_tag' => 'XYZ',
    'status_id' => 1,
    'model_id'   => 1,
];


//setup the request, you can also use CURLOPT_URL
$ch = curl_init('http://my-server-hostname-was-inserted-here/api/v1/hardware/1');

// Returns the data/output as a string instead of raw data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Good practice to let people know who's accessing their servers. See https://en.wikipedia.org/wiki/User_agent
curl_setopt($ch, CURLOPT_USERAGENT, 'YourScript/0.1 (contact@email)');

// Spec
curl_setopt($ch,CURLOPT_POST,1);

//Set your auth headers
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Authorization: Bearer MY_API_TOKEN_WAS_INSERTED_HERE' 
    ));

// Send the $data fields
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);

// get stringified data/output. See CURLOPT_RETURNTRANSFER
$output = curl_exec($ch);


// Do something with the returned data. Here we're just dumping it to the screen.
print_r($output);

// get info about the request
$info = curl_getinfo($ch);

// close curl resource to free up system resources 
curl_close($ch)
?>

Actual Behavior

I get a 302 redirect in the apache log and see this html:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="refresh" content="0;url=http://localhost" />

        <title>Redirecting to http://localhost</title>
    </head>
    <body>
        Redirecting to <a href="http://localhost">http://localhost</a>.
    </body>
</html>

Please confirm you have done the following before posting your bug report:


Please provide answers to these questions before posting your bug report:

  • Version of Snipe-IT you're running

Git pulled the 4.0-beta tag

  • What OS and web server you're running Snipe-IT on

Ubuntu 16.04

  • What method you used to install Snipe-IT (install.sh, manual installation, docker, etc)

Docker

  • WITH DEBUG TURNED ON, if you're getting an error in your browser, include that error

N/A (API)

  • What specific Snipe-IT page you're on, and what specific element you're interacting with to trigger the error

N/A (API)

  • If a stacktrace is provided in the error, include that too.

No stacktrace is provided (Just a 302 redirect)

  • Any errors that appear in your browser's error console.

N/A (API)

N/A (API, looks like the demo site isn't on 4.0-beta)

  • Include any additional information you can find in app/storage/logs and your webserver's logs.

There are no logs generated when I perform this API call

  • Include what you've done so far in the installation, and if you got any error messages along the way.
  1. Built a docker image from 4.0-beta
  2. Created a mysql database
  3. Deployed the container with appropriate environment variables
  4. Ran the post-install steps that were indicated (https://github.com/snipe/snipe-it/releases/tag/v4.0-beta)
  5. ran through the the web installation (successful)
  6. Setup company, model and category
  7. Tried to run the API call listed above in PHP
  • Indicate whether or not you've manually edited any data directly in the database

No manual edits in the database (fresh install)

rcarterv avatar Aug 14 '17 16:08 rcarterv

After digging into this, it looks like the example in the API documentation does not work with 4.0-beta (endpoint wants JSON). I was able to get it to work with this example:

Python

import requests
import json

asset_model = {
        'asset_tag' : 'XYZ123',
        'status_id' : 1,
        'model_id' : 1
}

asset_model_json = json.dumps(asset_model)


headers = {'Content-Type':'application/json', 'Content-Length': str(len(asset_model_json)), 'Authorization' :'Bearer <TOKEN_HERE>'}
r = requests.post('http://localhost/api/v1/hardware', headers=headers, data=asset_model_json)

print r.text

My only suggestion is to have the API return something a little more useful than a 302 message when it gets an improperly formatted submission. Aside from that, we're loving the beta: keep up the great work!

rcarterv avatar Aug 15 '17 17:08 rcarterv

Yeah, sorry about that - the sample code isn't really up to date yet.

The endpoint doesn't need JSON (it only returns JSON). You can explore the API using something like Postman and see that you can simply post fields.

We'll be updating the sample code so that it actually works. We've been a little busy with bugfixes before the release. :)

snipe avatar Aug 22 '17 21:08 snipe