Question

error_message":"Please send your attributes wrapped in 'asset'

  • 8 November 2023
  • 8 replies
  • 96 views

I am using the below code to upload asset to CS with ad header. But I am geeting the error.

 

error_message":"Please send your attributes wrapped in 'asset'

 

responseFromCsAsset = asset.addHeader("Content-Type", "image/jpeg")    .uploadAsset("myfilepath", myDesc).execute();

 

Please suggest !


8 replies

Hi @lalatendu,

 

Which language are you writing that in / which SDK are you using?

I’m assuming Java seeing the format?

 

This error message means that there should be an ‘asset’ object at the top, which is often forgotten (you should not just send the fields at the root).

So the JSON payload should look like:

{

  “asset’: {...}

}

 

Can you share the lines before this one for us to see how the asset object is constructed?

Hi @Victor Monsch 

Thanks for your quick response. Yes, I am using Java - here is the code snippet below.

 

Contentstack cs = new com.contentstack.cms.Contentstack.Builder().build();
Asset asset = cs.stack(apiKey, managementToken).asset();
responseFromCsAsset = asset.addHeader("Content-Type", "image/jpeg")    .uploadAsset("myfilepath", myDesc).execute();

So basically what I observed is I am able to upload Asset through Java using CMS , when I am not setting the Header. (Below is the working code)

responseFromCsAsset = asset.uploadAsset("myfilepath", myDesc).execute();

 

But in this case, the Image getting uploaded looks like a Doc like this in Content Stack.

Kindly advise !

@shailesh Mishra Expert advise please ?

Userlevel 1

Hello @lalatendu,

It should work without the custom header you are attempting to provide. Essentially, it should automatically determine the file type you are trying to upload. The code below should function as expected, but if you encounter any issues, please don't hesitate to let me know so that we can address and resolve them.

Contentstack cs = new com.contentstack.cms.Contentstack.Builder().build();
Asset asset = cs.stack(apiKey, managementToken).asset();
asset.addParam("relative_urls", true);
asset.addParam("include_dimension", true);
asset.addHeader("api_key", API_KEY);
asset.addHeader("authorization", MANAGEMENT_TOKEN);
asset.addHeader("authtoken", AUTHTOKEN);
String filePath = "/Users/shaileshmishra/Desktop/pexels.png";
String description = "The calender has been placed to assets";
response = asset.uploadAsset(filePath, description).execute();


Thank you !

Can you open the image after it’s been uploaded (without the custom header)?

@shailesh Mishra 

Yes, it’s working without providing any custom header and I am able to upload Asset (Image) through Java program.

But when I see this uploaded Image in the Content stack it shows me a "DOC" type (Screenshot attached for reference) and when I am trying to open this image from Contentstack, rather than opening directly, the image is getting downloaded.

 

In the above screenshot the Image ‘download123.jpeg’ is uploaded by using CMS API where we can see it’s uploaded as a ‘DOC’ type.

Whereas the other image ‘p-310264111.jpeg’ I have uploaded from Postman directly and it’s showing correctly, even I can click and see the Image.

Please advise !

@Victor Monsch FYI and answar to your question 

Basically, I can see the "Type" field of the Asset is updating as multipart/form-data (screenshot below). 

NOTE: This particular image I have downloaded randomly from Google Images

@shailesh Mishra

 

@lalatendu as @shailesh Mishra mentioned, the request needs to be multipart/form-data (this is also the way Postman sends it).

You can even take a look at the underlying API endpoint to upload an asset, Postman uses a request like the following:

curl -X POST \
https://api.contentstack.io/v3/assets?include_dimension=true \
-H 'api_key: {api_key_of_your_stack}' \
-H 'authtoken: {your_authtoken}' \
-H 'cache-control: no-cache' \
-H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
-F 'asset[upload]=@{Filepath e.g., /C:/Users/abc/Desktop/Sample.png}' \
-F 'asset[parent_uid]={If you need to add this file under an existing asset folder, pass the UID of the parent folder.}' \
-F 'asset[title]={If needed, enter a title for your uploaded asset.}' \
-F 'asset[description]={If needed, enter a description for your uploaded asset.}'
-F 'asset[tags]={If needed, assign a specific tag to your uploaded asset.}'

You can try a few things here:

  • Upload the same image from Postman and your Java runtime, see if the result still differs
  • If it does, investigate further
    • Does the image with the wrong type download properly (you get the image again)
    • Look at the CURL request your Postman makes and compare it with the one above, it should be similar
    • Capture your network calls to see the HTTP request being made from your Java runtime via our SDK

Reply