API POST help

So I want to take advantage of the foreman API and found the API wiki
page. A lot of useful information I am sure, but I am not familiar
with REST which it seems foreman's API utilizes.

What I am trying to do is create a new host. We have an Orchestration
flow/process tool that can POST data to sites. Now, I do not know if
a form POST is the same as doing a POST to REST. If they are not the
same then I would guess that the tool we use does a web form POST, so
please keep this in mind when providing an example.

Now what I am trying to figure out is what exactly needs to be put in
the POST data textbox to get this to work. Since adding a host has a
number of fields perhaps it would be easier to give an example of
creating a new environment, since that is just {environment {"name":
string}}

I realize first that we would need to login to foreman to have this
work. I think we have that piece working so please assume that.

So any tips on where to start on trying to get this to work would be
greatly appreciated. If more information is required please let me
know.

Thanks,
Jake

You need to POST and send a json encoded string with the post.

You can watch your foreman logs to see how to do it. Just create a new host with the gui and capture the log and it should be a pretty good example.

You might also need to include username/password info too.

Corey Osman
corey@logicminds.biz

Green I.T and Datacenter Automation Specialist

··· On Nov 4, 2011, at 4:10 PM, Jake - USPS wrote:

So I want to take advantage of the foreman API and found the API wiki
page. A lot of useful information I am sure, but I am not familiar
with REST which it seems foreman’s API utilizes.

What I am trying to do is create a new host. We have an Orchestration
flow/process tool that can POST data to sites. Now, I do not know if
a form POST is the same as doing a POST to REST. If they are not the
same then I would guess that the tool we use does a web form POST, so
please keep this in mind when providing an example.

Now what I am trying to figure out is what exactly needs to be put in
the POST data textbox to get this to work. Since adding a host has a
number of fields perhaps it would be easier to give an example of
creating a new environment, since that is just {environment {“name”:
string}}

I realize first that we would need to login to foreman to have this
work. I think we have that piece working so please assume that.

So any tips on where to start on trying to get this to work would be
greatly appreciated. If more information is required please let me
know.

Thanks,
Jake


You received this message because you are subscribed to the Google Groups “Foreman users” group.
To post to this group, send email to foreman-users@googlegroups.com.
To unsubscribe from this group, send email to foreman-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/foreman-users?hl=en.

> So I want to take advantage of the foreman API and found the API wiki
> page. A lot of useful information I am sure, but I am not familiar
> with REST which it seems foreman's API utilizes.
>
> What I am trying to do is create a new host. We have an Orchestration
> flow/process tool that can POST data to sites. Now, I do not know if
> a form POST is the same as doing a POST to REST. If they are not the
> same then I would guess that the tool we use does a web form POST, so
> please keep this in mind when providing an example.
>
> Now what I am trying to figure out is what exactly needs to be put in
> the POST data textbox to get this to work. Since adding a host has a
> number of fields perhaps it would be easier to give an example of
> creating a new environment, since that is just {environment {"name":
> string}}
>
> I realize first that we would need to login to foreman to have this
> work. I think we have that piece working so please assume that.
>
> So any tips on where to start on trying to get this to work would be
> greatly appreciated. If more information is required please let me
> know.
>

sending a form and submit via the rest api is more or less the same,
however the subtile differences.

the http request headers should include tro things:
content - type should be json
basic authentication - should send your username / password

there are a few examples in Feature #945: foreman CLI - Hammer CLI - Foreman,
for example: https://gist.github.com/1229194

note that this uses the fact that you can store almost all of the host
attribute in a hostgroup (as a template), also note it was probably
tested on 0.4 (which auto set the ipaddress among other things in this
example).

Ohad

··· On Sat, Nov 5, 2011 at 1:10 AM, Jake - USPS wrote: > Thanks, > Jake > > -- > You received this message because you are subscribed to the Google Groups "Foreman users" group. > To post to this group, send email to foreman-users@googlegroups.com. > To unsubscribe from this group, send email to foreman-users+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/foreman-users?hl=en. > >

Ohad,

Thanks a ton. The content-type got it working for us! After that
using the setups as documented in the API exactly worked just fine.

Regards,
Jake

··· On Nov 4, 10:32 pm, Ohad Levy wrote: > On Sat, Nov 5, 2011 at 1:10 AM, Jake - USPS wrote: > > > > > > > > > > > So I want to take advantage of the foreman API and found the API wiki > > page. A lot of useful information I am sure, but I am not familiar > > with REST which it seems foreman's API utilizes. > > > What I am trying to do is create a new host. We have an Orchestration > > flow/process tool that can POST data to sites. Now, I do not know if > > a form POST is the same as doing a POST to REST. If they are not the > > same then I would guess that the tool we use does a web form POST, so > > please keep this in mind when providing an example. > > > Now what I am trying to figure out is what exactly needs to be put in > > the POST data textbox to get this to work. Since adding a host has a > > number of fields perhaps it would be easier to give an example of > > creating a new environment, since that is just {environment {"name": > > string}} > > > I realize first that we would need to login to foreman to have this > > work. I think we have that piece working so please assume that. > > > So any tips on where to start on trying to get this to work would be > > greatly appreciated. If more information is required please let me > > know. > > sending a form and submit via the rest api is more or less the same, > however the subtile differences. > > the http request headers should include tro things: > content - type should be json > basic authentication - should send your username / password > > there are a few examples inhttp://theforeman.org/issues/945, > for example:https://gist.github.com/1229194 > > note that this uses the fact that you can store almost all of the host > attribute in a hostgroup (as a template), also note it was probably > tested on 0.4 (which auto set the ipaddress among other things in this > example). > > Ohad > > > > > > > > > Thanks, > > Jake > > > -- > > You received this message because you are subscribed to the Google Groups "Foreman users" group. > > To post to this group, send email to foreman-users@googlegroups.com. > > To unsubscribe from this group, send email to foreman-users+unsubscribe@googlegroups.com. > > For more options, visit this group athttp://groups.google.com/group/foreman-users?hl=en.

I'll add my thanks to Jake's. He was helping me on this issue.
The literal repair was to put "application/json" in the "contentType"
input field. This allowed us to clear the first hurdle (POST new
host),
now onto the next.

We use a parameter to store an IP address for a second network
interface. I'm attempting to POST to /common_parameters:

{commonparameter : {"name":bu_ip, "reference_id":1,
"type":HostParameter, "value":192.192.192.192}}

Log file results:

Processing CommonParametersController#create (for 56.192.77.198 at
2011-11-10 11:24:53) [POST]
Parameters: {"commonparameter"=>{"name"=>"bu_ip",
"value"=>"192.192.192.192", "type"=>"HostParameter",
"reference_id"=>1}}
Failed to save: Name can't be blank, Name can't be blank or contain
trailing white space, Value can't be blank, Value can't be blank or
contain trailing white space
Completed in 39ms (View: 8, DB: 10) | 422 Unprocessable Entity
[https://eagnmnmbpe4f.usps.gov/common_parameters]

PLEASE NOTE: Jake asked me to add that we are currently running
version .3

Suggestions?

Thanks, Kevin

··· On Nov 8, 12:58 pm, jmccann wrote: > Ohad, > > Thanks a ton. The content-type got it working for us! After that > using the setups as documented in the API exactly worked just fine. > > Regards, > Jake > > On Nov 4, 10:32 pm, Ohad Levy wrote: > > > > > > > > > On Sat, Nov 5, 2011 at 1:10 AM, Jake - USPS wrote: > > > > So I want to take advantage of the foreman API and found the API wiki > > > page. A lot of useful information I am sure, but I am not familiar > > > with REST which it seems foreman's API utilizes. > > > > What I am trying to do is create a new host. We have an Orchestration > > > flow/process tool that can POST data to sites. Now, I do not know if > > > a form POST is the same as doing a POST to REST. If they are not the > > > same then I would guess that the tool we use does a web form POST, so > > > please keep this in mind when providing an example. > > > > Now what I am trying to figure out is what exactly needs to be put in > > > the POST data textbox to get this to work. Since adding a host has a > > > number of fields perhaps it would be easier to give an example of > > > creating a new environment, since that is just {environment {"name": > > > string}} > > > > I realize first that we would need to login to foreman to have this > > > work. I think we have that piece working so please assume that. > > > > So any tips on where to start on trying to get this to work would be > > > greatly appreciated. If more information is required please let me > > > know. > > > sending a form and submit via the rest api is more or less the same, > > however the subtile differences. > > > the http request headers should include tro things: > > content - type should be json > > basic authentication - should send your username / password > > > there are a few examples inhttp://theforeman.org/issues/945, > > for example:https://gist.github.com/1229194 > > > note that this uses the fact that you can store almost all of the host > > attribute in a hostgroup (as a template), also note it was probably > > tested on 0.4 (which auto set the ipaddress among other things in this > > example). > > > Ohad > > > > Thanks, > > > Jake > > > > -- > > > You received this message because you are subscribed to the Google Groups "Foreman users" group. > > > To post to this group, send email to foreman-users@googlegroups.com. > > > To unsubscribe from this group, send email to foreman-users+unsubscribe@googlegroups.com. > > > For more options, visit this group athttp://groups.google.com/group/foreman-users?hl=en.

> I'll add my thanks to Jake's. He was helping me on this issue.
> The literal repair was to put "application/json" in the "contentType"
> input field. This allowed us to clear the first hurdle (POST new
> host),
> now onto the next.
>
> We use a parameter to store an IP address for a second network
> interface. I'm attempting to POST to /common_parameters:
>
> {commonparameter : {"name":bu_ip, "reference_id":1,
> "type":HostParameter, "value":192.192.192.192}}
>
> Log file results:
>
> Processing CommonParametersController#create (for 56.192.77.198 at
> 2011-11-10 11:24:53) [POST]
> Parameters: {"commonparameter"=>{"name"=>"bu_ip",
> "value"=>"192.192.192.192", "type"=>"HostParameter",
> "reference_id"=>1}}
> Failed to save: Name can't be blank, Name can't be blank or contain
> trailing white space, Value can't be blank, Value can't be blank or
> contain trailing white space
> Completed in 39ms (View: 8, DB: 10) | 422 Unprocessable Entity
> [https://eagnmnmbpe4f.usps.gov/common_parameters]
>
> PLEASE NOTE: Jake asked me to add that we are currently running
> version .3
>
> Suggestions?
Parameters should be sent along with the original request to create /
update the host.
if you look at your logs, it usually would be something like
(completely from memory)

host …{ :host_parameter_attributes => [:uid => {:value, :name…},
:uid2 => {:value

Ohad

··· On Thu, Nov 10, 2011 at 7:35 PM, kevin.dover@usps.gov wrote: > > Thanks, Kevin > > On Nov 8, 12:58 pm, jmccann wrote: >> Ohad, >> >> Thanks a ton. The content-type got it working for us! After that >> using the setups as documented in the API exactly worked just fine. >> >> Regards, >> Jake >> >> On Nov 4, 10:32 pm, Ohad Levy wrote: >> >> >> >> >> >> >> >> > On Sat, Nov 5, 2011 at 1:10 AM, Jake - USPS wrote: >> >> > > So I want to take advantage of the foreman API and found the API wiki >> > > page. A lot of useful information I am sure, but I am not familiar >> > > with REST which it seems foreman's API utilizes. >> >> > > What I am trying to do is create a new host. We have an Orchestration >> > > flow/process tool that can POST data to sites. Now, I do not know if >> > > a form POST is the same as doing a POST to REST. If they are not the >> > > same then I would guess that the tool we use does a web form POST, so >> > > please keep this in mind when providing an example. >> >> > > Now what I am trying to figure out is what exactly needs to be put in >> > > the POST data textbox to get this to work. Since adding a host has a >> > > number of fields perhaps it would be easier to give an example of >> > > creating a new environment, since that is just {environment {"name": >> > > string}} >> >> > > I realize first that we would need to login to foreman to have this >> > > work. I think we have that piece working so please assume that. >> >> > > So any tips on where to start on trying to get this to work would be >> > > greatly appreciated. If more information is required please let me >> > > know. >> >> > sending a form and submit via the rest api is more or less the same, >> > however the subtile differences. >> >> > the http request headers should include tro things: >> > content - type should be json >> > basic authentication - should send your username / password >> >> > there are a few examples inhttp://theforeman.org/issues/945, >> > for example:https://gist.github.com/1229194 >> >> > note that this uses the fact that you can store almost all of the host >> > attribute in a hostgroup (as a template), also note it was probably >> > tested on 0.4 (which auto set the ipaddress among other things in this >> > example). >> >> > Ohad >> >> > > Thanks, >> > > Jake >> >> > > -- >> > > You received this message because you are subscribed to the Google Groups "Foreman users" group. >> > > To post to this group, send email to foreman-users@googlegroups.com. >> > > To unsubscribe from this group, send email to foreman-users+unsubscribe@googlegroups.com. >> > > For more options, visit this group athttp://groups.google.com/group/foreman-users?hl=en. > > -- > You received this message because you are subscribed to the Google Groups "Foreman users" group. > To post to this group, send email to foreman-users@googlegroups.com. > To unsubscribe from this group, send email to foreman-users+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/foreman-users?hl=en. > >

Thanks for pointing us in the right direction again. We got it to
work.

Thanks,
Jake

··· On Nov 10, 1:39 pm, Ohad Levy wrote: > On Thu, Nov 10, 2011 at 7:35 PM, kevin.do...@usps.gov > > > > > > > > wrote: > > I'll add my thanks to Jake's. He was helping me on this issue. > > The literal repair was to put "application/json" in the "contentType" > > input field. This allowed us to clear the first hurdle (POST new > > host), > > now onto the next. > > > We use a parameter to store an IP address for a second network > > interface. I'm attempting to POST to /common_parameters: > > > {commonparameter : {"name":bu_ip, "reference_id":1, > > "type":HostParameter, "value":192.192.192.192}} > > > Log file results: > > > Processing CommonParametersController#create (for 56.192.77.198 at > > 2011-11-10 11:24:53) [POST] > > Parameters: {"commonparameter"=>{"name"=>"bu_ip", > > "value"=>"192.192.192.192", "type"=>"HostParameter", > > "reference_id"=>1}} > > Failed to save: Name can't be blank, Name can't be blank or contain > > trailing white space, Value can't be blank, Value can't be blank or > > contain trailing white space > > Completed in 39ms (View: 8, DB: 10) | 422 Unprocessable Entity > > [https://eagnmnmbpe4f.usps.gov/common_parameters] > > > PLEASE NOTE: Jake asked me to add that we are currently running > > version .3 > > > Suggestions? > > Parameters should be sent along with the original request to create / > update the host. > if you look at your logs, it usually would be something like > (completely from memory) > > host ..{ :host_parameter_attributes => [:uid => {:value, :name....}, > :uid2 => {:value > > Ohad > > > > > > > > > > > Thanks, Kevin > > > On Nov 8, 12:58 pm, jmccann wrote: > >> Ohad, > > >> Thanks a ton. The content-type got it working for us! After that > >> using the setups as documented in the API exactly worked just fine. > > >> Regards, > >> Jake > > >> On Nov 4, 10:32 pm, Ohad Levy wrote: > > >> > On Sat, Nov 5, 2011 at 1:10 AM, Jake - USPS wrote: > > >> > > So I want to take advantage of the foreman API and found the API wiki > >> > > page. A lot of useful information I am sure, but I am not familiar > >> > > with REST which it seems foreman's API utilizes. > > >> > > What I am trying to do is create a new host. We have an Orchestration > >> > > flow/process tool that can POST data to sites. Now, I do not know if > >> > > a form POST is the same as doing a POST to REST. If they are not the > >> > > same then I would guess that the tool we use does a web form POST, so > >> > > please keep this in mind when providing an example. > > >> > > Now what I am trying to figure out is what exactly needs to be put in > >> > > the POST data textbox to get this to work. Since adding a host has a > >> > > number of fields perhaps it would be easier to give an example of > >> > > creating a new environment, since that is just {environment {"name": > >> > > string}} > > >> > > I realize first that we would need to login to foreman to have this > >> > > work. I think we have that piece working so please assume that. > > >> > > So any tips on where to start on trying to get this to work would be > >> > > greatly appreciated. If more information is required please let me > >> > > know. > > >> > sending a form and submit via the rest api is more or less the same, > >> > however the subtile differences. > > >> > the http request headers should include tro things: > >> > content - type should be json > >> > basic authentication - should send your username / password > > >> > there are a few examples inhttp://theforeman.org/issues/945, > >> > for example:https://gist.github.com/1229194 > > >> > note that this uses the fact that you can store almost all of the host > >> > attribute in a hostgroup (as a template), also note it was probably > >> > tested on 0.4 (which auto set the ipaddress among other things in this > >> > example). > > >> > Ohad > > >> > > Thanks, > >> > > Jake > > >> > > -- > >> > > You received this message because you are subscribed to the Google Groups "Foreman users" group. > >> > > To post to this group, send email to foreman-users@googlegroups.com. > >> > > To unsubscribe from this group, send email to foreman-users+unsubscribe@googlegroups.com. > >> > > For more options, visit this group athttp://groups.google.com/group/foreman-users?hl=en. > > > -- > > You received this message because you are subscribed to the Google Groups "Foreman users" group. > > To post to this group, send email to foreman-users@googlegroups.com. > > To unsubscribe from this group, send email to foreman-users+unsubscribe@googlegroups.com. > > For more options, visit this group athttp://groups.google.com/group/foreman-users?hl=en.

This has now been successfully tested in versions .3 and .4.

HP Operations Orchestration (OO) Studio - workflow tool
Http client post raw
inputs:
url - https://${Foreman Env}:8443/hosts
postDocument - {host : {"architecture_id":1, "domain_id":1,
"environment_id":6, "ip":56.56.56.56, "mac":005056bd5376, "name":blush:
{Hostname}, "operatingsystem_id":3, "ptable_id":10,
"host_parameters_attributes" : { "0" :
{ "name":bu_ip,"nested":"","value":192.168.109.236,"_destroy":""}} }}
contentType - application/json

The key to debugging was to watch the log when manually creating
hosts, then attempting replication within OO.

Thanks for your support, Kevin

··· On Nov 15, 8:05 am, jmccann wrote: > Thanks for pointing us in the right direction again. We got it to > work. > > Thanks, > Jake > > On Nov 10, 1:39 pm, Ohad Levy wrote: > > > > > > > > > On Thu, Nov 10, 2011 at 7:35 PM, kevin.do...@usps.gov > > > wrote: > > > I'll add my thanks to Jake's. He was helping me on this issue. > > > The literal repair was to put "application/json" in the "contentType" > > > input field. This allowed us to clear the first hurdle (POST new > > > host), > > > now onto the next. > > > > We use a parameter to store an IP address for a second network > > > interface. I'm attempting to POST to /common_parameters: > > > > {commonparameter : {"name":bu_ip, "reference_id":1, > > > "type":HostParameter, "value":192.192.192.192}} > > > > Log file results: > > > > Processing CommonParametersController#create (for 56.192.77.198 at > > > 2011-11-10 11:24:53) [POST] > > > Parameters: {"commonparameter"=>{"name"=>"bu_ip", > > > "value"=>"192.192.192.192", "type"=>"HostParameter", > > > "reference_id"=>1}} > > > Failed to save: Name can't be blank, Name can't be blank or contain > > > trailing white space, Value can't be blank, Value can't be blank or > > > contain trailing white space > > > Completed in 39ms (View: 8, DB: 10) | 422 Unprocessable Entity > > > [https://eagnmnmbpe4f.usps.gov/common_parameters] > > > > PLEASE NOTE: Jake asked me to add that we are currently running > > > version .3 > > > > Suggestions? > > > Parameters should be sent along with the original request to create / > > update the host. > > if you look at your logs, it usually would be something like > > (completely from memory) > > > host ..{ :host_parameter_attributes => [:uid => {:value, :name....}, > > :uid2 => {:value > > > Ohad > > > > Thanks, Kevin > > > > On Nov 8, 12:58 pm, jmccann wrote: > > >> Ohad, > > > >> Thanks a ton. The content-type got it working for us! After that > > >> using the setups as documented in the API exactly worked just fine. > > > >> Regards, > > >> Jake > > > >> On Nov 4, 10:32 pm, Ohad Levy wrote: > > > >> > On Sat, Nov 5, 2011 at 1:10 AM, Jake - USPS wrote: > > > >> > > So I want to take advantage of the foreman API and found the API wiki > > >> > > page. A lot of useful information I am sure, but I am not familiar > > >> > > with REST which it seems foreman's API utilizes. > > > >> > > What I am trying to do is create a new host. We have an Orchestration > > >> > > flow/process tool that can POST data to sites. Now, I do not know if > > >> > > a form POST is the same as doing a POST to REST. If they are not the > > >> > > same then I would guess that the tool we use does a web form POST, so > > >> > > please keep this in mind when providing an example. > > > >> > > Now what I am trying to figure out is what exactly needs to be put in > > >> > > the POST data textbox to get this to work. Since adding a host has a > > >> > > number of fields perhaps it would be easier to give an example of > > >> > > creating a new environment, since that is just {environment {"name": > > >> > > string}} > > > >> > > I realize first that we would need to login to foreman to have this > > >> > > work. I think we have that piece working so please assume that. > > > >> > > So any tips on where to start on trying to get this to work would be > > >> > > greatly appreciated. If more information is required please let me > > >> > > know. > > > >> > sending a form and submit via the rest api is more or less the same, > > >> > however the subtile differences. > > > >> > the http request headers should include tro things: > > >> > content - type should be json > > >> > basic authentication - should send your username / password > > > >> > there are a few examples inhttp://theforeman.org/issues/945, > > >> > for example:https://gist.github.com/1229194 > > > >> > note that this uses the fact that you can store almost all of the host > > >> > attribute in a hostgroup (as a template), also note it was probably > > >> > tested on 0.4 (which auto set the ipaddress among other things in this > > >> > example). > > > >> > Ohad > > > >> > > Thanks, > > >> > > Jake > > > >> > > -- > > >> > > You received this message because you are subscribed to the Google Groups "Foreman users" group. > > >> > > To post to this group, send email to foreman-users@googlegroups.com. > > >> > > To unsubscribe from this group, send email to foreman-users+unsubscribe@googlegroups.com. > > >> > > For more options, visit this group athttp://groups.google.com/group/foreman-users?hl=en. > > > > -- > > > You received this message because you are subscribed to the Google Groups "Foreman users" group. > > > To post to this group, send email to foreman-users@googlegroups.com. > > > To unsubscribe from this group, send email to foreman-users+unsubscribe@googlegroups.com. > > > For more options, visit this group athttp://groups.google.com/group/foreman-users?hl=en.