0

I need to update a shell script for work that passes in some parameters via curl, like so

curl -X POST -H 'Content-Type: application/json' "http://myurl?param1=test1&param2=test2&param3=test3"

That all works fine, but the problem is my API needs a fourth parameter that isn't just a string, it's a Java object defined within our API. Is there any was I can manage that with a shell script? Appreciate any help. I'm fairly new to Java so I'm floundering a little bit.

4
  • UNIX commands' arguments can only ever be C strings (that is to say, NUL-terminated strings), no matter what language you call them through. URLs are also strings. Commented Dec 15, 2020 at 22:46
  • ...so this Java object will need to be serialized to a string before it can be passed. Documentation for the API you're calling should describe whatever kind of serialization it is you need (whether that's some kind of marshalling to JSON, or passing an identifier used on the server side to look up the object, or whatever else it may be). Commented Dec 15, 2020 at 22:46
  • 1
    Thus, the first place to start is to read the documentation for your API and figure out what it needs; only after you've made that determination can you ask a question with the specifics we need to help you. (If the API is one you're writing yourself, then it's your job to deserialize the data coming in back into an object, or look up that object in a store, or take whatever other action is appropriate; be mindful in making design decisions that not all serialization formats are intended for safely handling untrusted data). Commented Dec 15, 2020 at 22:48
  • Are there any preexisting clients for this API? A good place to start would be looking at what they do. If there's a browser-based client, for example, use your browser's built-in debugger to look at what arguments it sends to the server, and you'll be able to see how it represents the data at hand in string form. Commented Dec 15, 2020 at 22:54

1 Answer 1

1

Two possible options, among a lot of others.


Base64 + byte[]

  1. Send

    • serialize the Object to byte[].

    • encode to base64.

      ByteArrayOutputStream bos= new ByteArrayOutputStream();
      ObjectOutputStream obs = new ObjectOutputStream(bos);
      obs.writeObject(yourJavaObject);
      obs.flush();
      curlStringObj= new String(Base64.encode(bos.toByteArray()));       
      

The curlStringObj string should be the one you send in the curl call.

  1. Receive

    • decode from base64

    • deserialize the byte[] to Object

      byte b[] = Base64.decode(curlStringObj.getBytes()); 
      ByteArrayInputStream bis= new ByteArrayInputStream(b);
      ObjectInputStream ois = new ObjectInputStream(bis);
      YourJavaObject receivedJavaObject= (YourJavaObject) ois.readObject();
      

GSON (Json)

Another option is to serialize it to JSON, for example, using the GSON lib. Probably one of the simplest ways.

  1. Send

     Gson gson = new Gson();
     String curlStringObj = gson.toJson(yourJavaObject);
    
  2. Receive

     Gson gson = new Gson(); 
     YourJavaObject jObject = gson.fromJson(curlStringObj ,YourJavaObject.class);
    

Anyway, just take these as examples. Check first the API's specification and convert the object according to it. Hope it helps somehow.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.