Skip to Content
0

Image from oData is not fetched

Feb 03 at 06:45 PM

103

avatar image

Hi

I am trying to replicate Shopping cart with one of the few oData through SAP SDK for iOS.

I am able to fetch all the details but somehow Material Image is not getting fetched . The pathof Url in Odata is coming in parameter product.pctureurl as shown below:

"/SampleServices/ESPM.svc/resources/images/HT-1000.jpg"

func getDataFromUrl(url: URL, completion: @escaping (Data?, URLResponse?, Error?) -> ()) {URLSession.shared.dataTask(with: url) { data, response, error incompletion(data, response, error)}.resume()}

But every-time Image is Blank. The value of data?.count is 3358

If I pass a normal URL in url like

https://s2uqdnlqz93lrjbq205ld0eu-wpengine.netdna-ssl.com/wp-content/uploads/2016/07/Gala-single-nosticker.png

as shown below, the value of Data?.count is 71180

The Output is, layout can be adjusted later

Kindly suggest what I am doing wrong, I am using Xcode 9.2 Swift4

Thanks

Nilesh

code.png (110.5 kB)
data.png (78.9 kB)
normalurl.png (135.9 kB)
image-url.png (124.8 kB)
10 |10000 characters needed characters left characters exceeded

I think the issue here is that you are not successfully authenticated against the demo service endpoint and you get no valid image/* response, but a text/html response (you can check that in the reponse object), which cannot be converted to a UIImage of course. You can convert the data to a String to check the content, With the other URL it works, because no authentication is necessary and you get a valid image content type reponse.

But to give you more feedback it would be necessary to know a little bit more about your app. Are you using a mobile service application with a valid endpoint? Do you do any authentication logic against SCP? ...

If you have generated a proxy for the demo service in your app, you should use the downloadMedia API as suggested by Andreas. Constructing the URL manually with "$value" works, but is not the preferred way if a proxy is available. In addition your trial below with the "$value" addition is not correct, because you have constructed the wrong URL. You created a URL .../ESPM.svc/resources/images/HT-1000.jpg/$value which is wrong. If you do a GET request on the Products entity set you well see in the response a valid media link (for instance .../Products('HT-1000')/$value).

0

Hi Florian,

I am basically trying to built a replica of Shopping Cart App, so authentication logic is written during on boarding which was taken care by SDK . I have not generated proxy for this service as I am not really sure with the structure of this oData so instead I am trying to read image directly from oData and showing it in App. My question is, do i need to handle authentication again if once it happens during Start of the App ?. Since the oData sends

/SampleServices/ESPM.svc/resources/images/HT-1000.jpg in its parameter picturl and not the full Url. I have to build the whole URL again and tryng to fetch data from the URL. If I put this URL in Crome or any ither explorer it ask me Username and password and display me the image. I not very sure the correct approach to handle this scenario as I have just started learning swift .

Kindly Suggest the proper way to fetch image or handle this scenario.

Thanks

Nilesh

0
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Andreas Schlosser
Feb 05 at 07:06 AM
0

Nilesh,

Looks like you should maybe to a GET instead of POST to fetch the picture. For OData entities that have media resources, i.e. a $value property as Arjun is describing, you can use the `downloadMedia` API.

Thanks
Andreas

Show 5 Share
10 |10000 characters needed characters left characters exceeded

Hi Andreas,

I tried passing the same shown as below but result is still the same, I have changed httpMethod to GET as well, but somehow nothing happening.

0

Nilesh,

You shouldn't add the /$value to the picture URL, but to the entity you're working on, something like /Products('HT-1000')/$value

The OData framework would do this automatically for you, if you use the `downloadMedia` API of the DataService class.

Thanks
Andreas

0
Andreas Schlosser

Hi Andreas,

Could you share some link with such examples, I think I am missing something. I am not too sure about how to use

`downloadMedia` API of the DataService class , if you can provide some link I can go through this and try to understand how to use it.

Thanks

Nilesh

0
0
Andreas Schlosser

Thanks Andreas,

Will check and try .

Nilesh

0
Arjun Biswas Feb 05 at 04:43 AM
0

Hi Nilesh Mishra,

Could you try using $value with your URL. Usually you have to use this, when you are trying to fetch an media from the backend.

Regards,

Arjun Biswas

Show 2 Share
10 |10000 characters needed characters left characters exceeded

Hi Arjun,

I tried passing the same shown as below but result is still the same.

0

Hi Nilesh Mishra,

Could you debug and check in the backend, if the data is comming or nor.

Regards,

Arjun Biswas.

0