Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Call a REST API's POST method by passing input read from CSV file  RSS feed

 
Vivek Pinto
Greenhorn
Posts: 16
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
can anybody tell me how to call a Bulk REST API' POST method by passing the input stream reading from a csv file.
Looking out explanation on inputStream,buffered reader..write..response , and call the POST method.

Thank You


 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not quite sure what you're asking, or rather, I don't see the connection between handling a CSV file and a REST WS call. The WS si just server-side Java code - it can do anything any other Java code can do, including handling CSV files in whichever way you can imagine. But what do you mean by "passing the input stream from a CSV file"? Is the CSV file on the client? If so, you need to send its data as part of the WS call if the server needs to process it, an InputStream is not something you can pass between client and server.
 
Vivek Pinto
Greenhorn
Posts: 16
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:I'm not quite sure what you're asking, or rather, I don't see the connection between handling a CSV file and a REST WS call. The WS si just server-side Java code - it can do anything any other Java code can do, including handling CSV files in whichever way you can imagine. But what do you mean by "passing the input stream from a CSV file"? Is the CSV file on the client? If so, you need to send its data as part of the WS call if the server needs to process it, an InputStream is not something you can pass between client and server.


Dear Dittmer,

I am new bie to this.Trying for the first time.
In my question when i said input stream,i meant stream of data and not inputStream object.I am reading data from CSV and passing to POST method.

Let me put it in this way ,if you can understand ,then great.

I have a BULK REST API which would create or update Contact record (some thing like baseurl+contact/import/data).now i am trying to call that API...(Hope you understood till here).
Now the input for this API is the records read from a csv file.
I am looking for streaming the POST method...i.e write the data to POST method(Call to POST method of this API).(Hope you got what i am looking for)
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First off, my first name is Ulf. If you want to address me by my last name, then that's fine, but the polite way to do so is by prefixing it by "Mr". First name is fine, though.

OK, so you want to send the contents of the CSV file as data along with the REST call for processing on the server. You can do that in the body of a POST. The specifics will depend on how you've implemented the client side - can you post a relevant excerpt of the code you have so far?
 
Vivek Pinto
Greenhorn
Posts: 16
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the code..Please let me know if you guys find any need of change.
And i am getting "NULL" value for "connection.getOutputStream()".

 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what does this code do? What is the problem you're having with it?

Lines 8 to 20 seem to write something to a connection - that couldn't be the WS call, because the CSV isn't read until later. So whatever it does would seem unrelated to the issue at hand.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, you're getting an NPE. But the code does not show how you're obtaining the connection, so we can't speculate what might be going wrong.

And if that's the problem, please clarify how that block of code relates to the WS call (see my previous post).
 
Vivek Pinto
Greenhorn
Posts: 16
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:OK, you're getting an NPE. But the code does not show how you're obtaining the connection, so we can't speculate what might be going wrong.

And if that's the problem, please clarify how that block of code relates to the WS call (see my previous post).


Hi Ulf,
Sorry ,My Bad for calling you so.

You havent read my comment //in main ,setting up the data,reading from csv .
No problem..

Here is the complete code.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So there's a NPE in the line that contains "connection.getOutputStream()"? That's quite odd, as connection can't be null - several methods are invoked on it before that line. Please post the entire stack trace.

You should not close the OutputStreamWriter where you're doing that now, as closing it will close the underlying connection - close it when you're done reading as well.
 
Vivek Pinto
Greenhorn
Posts: 16
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:So there's a NPE in the line that contains "connection.getOutputStream()"? That's quite odd, as connection can't be null - several methods are invoked on it before that line. Please post the entire stack trace.

You should not close the OutputStreamWriter where you're doing that now, as closing it will close the underlying connection - close it when you're done reading as well.


this is the out put in console.
Seriously i don't know you are able to follow the problem?

URLhttps://secure.p03.eloqua.com/API/Bulk/1.0/contact/import/164/data
sun.net.www.protocol.https.DelegateHttpsURLConnection:https://secure.p03.eloqua.com/API/Bulk/1.0/contact/import/164/data
java.io.OutputStreamWriter@10564f7Connection get OutputStream :
Server Respose CODE=201

connection.getResponseMessage() : Created
sb.toString :syncedInstanceUri,syncStartedAt,syncEndedAt,status,createdAt,createdBy,uri/contact/import/164,,,pending,2014-03-05T08:45:43.5389415Z,Veeraraghavan.Srinivasan,/sync/413
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i don't know you are able to follow the problem?

Apparently not. Earlier you were talking about "getting "NULL" value for "connection.getOutputStream()". I took that to mean that a NPE occurred, but that's apparently not what you meant?

From the output you posted it would appear that the code runs without an exception. So what, exactly, is the problem?
 
Vivek Pinto
Greenhorn
Posts: 16
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.io.OutputStreamWriter@10564f7Connection get OutputStream :..........
is null..see the empty place .
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"null" is not the same as no data; please be precise about what's happening, we have only what you write to go by.

Are we still talking about the last code you posted? That looks a bit different - it couldn't have produced the exact output you posted just now. Printing either "wr.toString()" or "connection.getOutputStream()" is not particularly useful - what do you think those should print?

Given what is printed after that ("sb.toString..."), do you have reason to believe that something is going wrong? It looks OK to me.
 
Vivek Pinto
Greenhorn
Posts: 16
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:"null" is not the same as no data; please be precise about what's happening, we have only what you write to go by.

Are we still talking about the last code you posted? That looks a bit different - it couldn't have produced the exact output you posted just now. Printing either "wr.toString()" or "connection.getOutputStream()" is not particularly useful - what do you think those should print?

Given what is printed after that ("sb.toString..."), do you have reason to believe that something is going wrong? It looks OK to me.


Yes..Same here..that look ok.I am suspecting wrong inputs are being passed.

Well,If i figure out ,i will post.

Thank you for you valuable time Ulf.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!