# httpAPI null value Handling

**URL:** https://talk.remobjects.com/t/httpapi-null-value-handling/26261
**Category:** Data Abstract
**Created:** [February 28, 2022, 3:28am UTC](https://talk.remobjects.com/t/httpapi-null-value-handling/26261 "2022-02-28T03:28:23Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![jptechon](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@jptechon](https://talk.remobjects.com/u/jptechon)
#### Post date: [February 28, 2022, 3:28am UTC](https://talk.remobjects.com/t/httpapi-null-value-handling/26261/1 "2022-02-28T03:28:23Z")

</div>

Hi，

I used sjoson.WriteDataset(cdsResult, [woRows], -1) to stream data to restclient.

That dataset contain a TDatetime field but it is null value. I’ve defined a TComplexType return back the data say:

function updatedata(srcData : TmyComplexType);

As one of the data member(TDateTime) is null, it already raise exception on parsing the srcData before step into function call. It is exception of cannot convert null variants error.

Then I need at client side to check any null member and set a predefined datatime for null value.

At Server-Side I need check that value and change back to null.

Please advise any better Handling. Say all handling at server side , client side only get the value and return the value only.

Joe

---

<div class="post-metadata">

### Author: ![EvgenyK](https://talk.remobjects.com/user_avatar/talk.remobjects.com/evgenyk/32/16_2.png) [@EvgenyK](https://talk.remobjects.com/u/EvgenyK)
#### Post date: [February 28, 2022, 10:09am UTC](https://talk.remobjects.com/t/httpapi-null-value-handling/26261/2 "2022-02-28T10:09:34Z")

</div>

Hi,

are you using `TDAJSONDataStreamer`, is it correct? if yes, you can use events like `OnBeforeFieldValueSerialization`/`OnWriteFieldValue`/`OnWriteFieldValueEx` for writing `0` instead of `null` for such fields.

---

<div class="post-metadata">

### Author: ![jptechon](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@jptechon](https://talk.remobjects.com/u/jptechon)
#### Post date: [March 1, 2022, 3:34am UTC](https://talk.remobjects.com/t/httpapi-null-value-handling/26261/3 "2022-03-01T03:34:56Z")

</div>

Thanks it work.

However, I hit one issue is as some client is web site or even for outside people to call.

function updatedata(srcData : TmyComplexType);

Like this function， any chance I convert the datamember value first ?  
For example:  
web client : “post” updatedata (srcData)  
RO server: Convert null value  
RO server: process updatedata call

as someone may skip somedatamember( i.e use did not input any value) , it because null, when calling it go to exception again.

any chance I can convert it instead of throwing exception?

joe

---

<div class="post-metadata">

### Author: ![EvgenyK](https://talk.remobjects.com/user_avatar/talk.remobjects.com/evgenyk/32/16_2.png) [@EvgenyK](https://talk.remobjects.com/u/EvgenyK)
#### Post date: [March 1, 2022, 10:15am UTC](https://talk.remobjects.com/t/httpapi-null-value-handling/26261/4 "2022-03-01T10:15:28Z")

</div>

Hi,

as I understand, you can check values for null at the beginning of your method like

```auto
procedure TMyService.updatedata(srcData : TmyComplexType);
begin
  // check srcData's members for null and replace null values with accepted values
  // process std update 
end;

```

---

<div class="post-metadata">

### Author: ![jptechon](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@jptechon](https://talk.remobjects.com/u/jptechon)
#### Post date: [March 2, 2022, 3:03am UTC](https://talk.remobjects.com/t/httpapi-null-value-handling/26261/5 "2022-03-02T03:03:49Z")

</div>

The exception is before enerting the function. during the parsing of the parameter srcdata.

You can define TmyComplexType with TdateTime data member inside.

Then use postman to put null value to that datatime member. Then you can get the case.

please advise

---

<div class="post-metadata">

### Author: ![EvgenyK](https://talk.remobjects.com/user_avatar/talk.remobjects.com/evgenyk/32/16_2.png) [@EvgenyK](https://talk.remobjects.com/u/EvgenyK)
#### Post date: [March 2, 2022, 6:20am UTC](https://talk.remobjects.com/t/httpapi-null-value-handling/26261/6 "2022-03-02T06:20:18Z")

</div>

Hi,

HttpApi calls is configured to initialize missing fields with default values.  
Can you give to me example of your call with CURL, pls?

you can drop message to support@ for keeping this info in privacy

---

<div class="post-metadata">

### Author: ![jptechon](https://talk.remobjects.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@jptechon](https://talk.remobjects.com/u/jptechon)
#### Post date: [March 3, 2022, 6:13am UTC](https://talk.remobjects.com/t/httpapi-null-value-handling/26261/7 "2022-03-03T06:13:26Z")

</div>

You can refer below case:

> KB\_STRUCT = class(TROComplexType)  
> private  
> fKB\_ID\_PK: UnicodeString;  
> fKB\_DT: DateTime;  
> published  
> property KB\_ID\_PK: UnicodeString read fKB\_ID\_PK write fKB\_ID\_PK;  
> property KB\_DT: DateTime read fKB\_DT write fKB\_DT;
> 
> end;

> C:\Auto-Dvp\Tools\curl-7.63\bin\>C:\Auto-Dvp\Tools\curl-7.63\bin\curl -verbose -X POST “[http://10.18.18.64:8099/api/getentity](http://10.18.18.64:8099/api/getentity)” -H “accept: application/json” -H “Content-Type: application/json” -d “{ "IN\_KB":{ "KB\_DT": "null", "KB\_ID\_PK": "ID001" }}”  
> Error reading parameter IN\_KB: ‘null’ is not a valid integer value

Why I put KB\_DT is null it is becase this value is from DA server json.WriteDataset( (cdsTmpDataset as IDADataset), [woRows], -1);

Below is the call stack for that function.

 ![datetime-error](https://talk.remobjects.com/uploads/default/original/3X/d/e/def8575b0588ee1e72da0fb464492d3344131c10.png)

Also the attached the sample project for reference:  
[HttpAPITestServer.zip](https://talk.remobjects.com/uploads/short-url/a4JOkz1wKv9V1DVul0FVefevtKh.zip) (185.5 KB)

if I use writedataex, it can change all null value however this let client side do many extra work, like null value presentation in UI and null value post back to the server.

Is it any way that I can check value in server side when client post back before parsing the value into the server function call?

Please advise.

---

<div class="post-metadata">

### Author: ![EvgenyK](https://talk.remobjects.com/user_avatar/talk.remobjects.com/evgenyk/32/16_2.png) [@EvgenyK](https://talk.remobjects.com/u/EvgenyK)
#### Post date: [March 3, 2022, 2:45pm UTC](https://talk.remobjects.com/t/httpapi-null-value-handling/26261/8 "2022-03-03T14:45:38Z")

</div>

Hi,

better solution is omit null values for HTTPApi calls, like

```auto
>curl -verbose -X POST "http://localhost:8099/api/getentity" -H "accept: application/json" -H "Content-Type: application/json" -d "{ \"IN_KB\":{ \"KB_ID_PK\": \"ID001\" }}"
Note: Unnecessary use of -X or --request, POST is already inferred.
* Trying 127.0.0.1:8099...
* Connected to localhost (127.0.0.1) port 8099 (#0)
> POST /api/getentity HTTP/1.1
> Host: localhost:8099
> User-Agent: curl/7.72.0
> Referer: rbose
> accept: application/json
> Content-Type: application/json
> Content-Length: 35
>
* upload completely sent off: 35 out of 35 bytes
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Connection: close
< Content-Type: application/json; charset=utf-8
< Content-Length: 51
< Date: Thu, 03 Mar 2022 14:36:14 GMT
< Accept-Encoding: gzip, identity
<
{"KB_DT":"1899-12-29T21:00:00Z","KB_ID_PK":"ID001"}* Closing connection 0

```

> [@jptechon](#):
>
> Why I put KB\_DT is null it is becase this value is from DA server json.WriteDataset( (cdsTmpDataset as IDADataset), [woRows], -1);

here is a small difference - datastreamer treats all fields values as variants so those fields can have `null` values. in your case, you are using `Datatime` field that cannot be nullable.
