cancel
Showing results for 
Search instead for 
Did you mean: 

POST API Example Required

Highlighted

POST API Example Required

Hi Alan,

 

I am struggling with POST REST api's in Magento 2 as I am quiet new to it.

 

If its possible for you to provide me one post example like creation of product using oauth and admin account.

 

Or add to cart.

 

I put this query in programming questions too however i am in bit hurry that's why looking for quick solution.

 

Please please help me.

 

Thanks in Advance.

 
6 REPLIES 6
Highlighted

Re: POST API Example Required

Hi - I don't have a sample handy sorry, but some hopefully useful tip until then. 

 

  • Don't confuse our OAuth with normal user based OAuth. We use it for system-to-system authentication. The fact that it is OAuth based is actually irrelevant. 
  • For username/passwodrd authentication, which can be used by an administrator as well, http://devdocs.magento.com/guides/v2.0/get-started/authentication/gs-authentication-token.html#curl-... may be useful reading. It includes a CURL command doing a POST to provide username/password and get back an authentication token.
  • If you look at https://github.com/magento/magento2/blob/develop/app/code/Magento/Catalog/etc/webapi.xml#L12 you will see POSTing to /V1/products will call the "save()" method (that is, create a new product).
  • The question next then is how to form the JSON to POST. http://devdocs.magento.com/guides/v2.0/get-started/gs-web-api-request.html talks about how to form the headers (including the authentication token). It also has an example of creating a Customer object. Instead, you would look at the ProductInterface definition, or look at the swagger documentation. 
  • For Swagger, http://devdocs.magento.com/swagger/, go down page to catalogProductRepositoryV1 and export the POST section. You will see you need to POST JSON something like: {"product":{"sku":"xxx","name:"xxx",....}}. The "product" corresponds to the function argument name. The other fields inside correspond to setters/getters on the ProductInterface definition. (The "id" is optional from memory - normally leave it out and Magento will allocate one. Specify it if you are trying to update an existing product definition.)

I don't have sample code or CURLs for each of the above, but hopefully this helps a bit!

Highlighted

Re: POST API Example Required

Hi Alan,

 

Pardon the intrusion on someone's question. I also want to thank you for the opportunity to have questions asked - I know a lot of questions can be way out of scope/place here but I do have something to ask in regards to the links you posted in particular on http://devdocs.magento.com/guides/v2.0/get-started/gs-web-api-request.html

 

Now I've previously been able to form/post-put/get info via the Soapv2 method in Magento 1.x fine, but that all seems to have been changed in M2.x. Two things I'm trying to wrap my head around:

 

For instance in the link referenced above, this assumes that you'll be making those calls from within the magento installation folder correct? For instance the $request method and creation of the curl request or am I reading this wrong? And if my assumption is correct (that these examples have to run from within the M2 installation), are there any methods to make these calls from outside of the M2 installation - even remotely?

 

Second, most of the examples reference the "app/code/Magento" structure, but everything seems to have been moved to "vendor/magento/module-xxxxx" - is this staying this way?

 

Thanks for any tips on this Alan!

 

Wlad

Highlighted

Re: POST API Example Required

That part of the docs is new to me (the team is chugging away at fleshing it all out). That particular code is sample code showing how you could use the Zend framework library to form a request to send to us, setting the headers etc. I think some intro text might help set it in context. There is no requirement to make requests from inside our code base - that would actually be unusual. The docs was trying to give an example of how you could build it in your own application, using an example based on Zend. I think a bit more intro text would help make the context clearer.

 

As to your second question, in GitHub the files are under app/code. But you are correct when you install with Composer they will sit under the vendor directory. The idea is the vendor directory is stuff you don't touch, as per Composer recommendations. The files are the same however. In my mind app/code is now for "local customizations", not official Magento shipped code. When you are developing the code base (like our internal developers, or a community member submitting a pull request) then all our code is "custom". When we release it for external projects, we bundle into Composer packages that others can then use - that packaging stage is when they move from "app/code" to "vendor". Yes, it is staying this way.

Highlighted

Re: POST API Example Required

Thank you again for taking the time to clarify that. It actually all makes sense now, or at least it confirms some of my thinking. Especially when it comes to the examples. It so happens that I'm (re)building our backend on the Zend framework which in turn will talk back and forth with our M2 installation so I'm excited about that.

 

With some trial and error I've been able to pull orders from our M2 installation and I'm so close to having a workable solution to update them too; I'm not too familiar with M2 yet, so having documentation would've helped speed up that learning process a bit.

 

Hopefully there will be some expanded documentation on the M2 API in the near future!

Highlighted

Re: POST API Example Required

Regarding API documentation, have you see the swagger docs?  Not perfect, but a possibly useful resource. http://devdocs.magento.com/swagger/ Also if you are using SOAP, is the returned WSDL file useful for docs?  Do do you find you need more "tutorial" style content, talking about how to use the specific API in greater depth?  Just wondering what concrete area we most need to improve on here.

 

Highlighted

Re: POST API Example Required

I actually found one of your posts a few days ago and another one on the breakdown of modules which helped a lot. I'm still on the fence between SOAP and REST methods. I'm using both for testing but seem to have more "luck" with REST. Ideally I would like to move everything over using SOAP so anything similar to the dev docs (SOAP implementation) for M1 would help a lot.  I was completely new to Magento (1.x) and with the available docs I had a fully functional backend within 4 hours!

 

I don't necessarily think that "tutorial" style content would help the Magento team much (considering time and maintenance etc); anything similar to Mv1 docs with samples on the API's can be a  big help - at least for me - and it helps to keep the clutter down.  The v1 docs are perfect compared to the Zend style tutorials (see Album, Blog etc) which can get outdated and may cause confusion to some if it isn't kept up.